Java 图像不会更改,如何刷新面板使其更改?

Java 图像不会更改,如何刷新面板使其更改?,java,swing,jpanel,jlabel,imageicon,Java,Swing,Jpanel,Jlabel,Imageicon,当某个事件发生时,我希望图像发生更改,但它没有发生。 代码如下: game.add(bbol); if (flashed == 1) { bbol.setIcon(bboH); } else { } 我需要刷新游戏面板吗 编辑: import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import

当某个事件发生时,我希望图像发生更改,但它没有发生。
代码如下:

game.add(bbol);
if (flashed == 1) {
   bbol.setIcon(bboH);
} else {
}
我需要刷新
游戏
面板吗

编辑:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.Border;

public class BeeBee extends JFrame implements ActionListener, MouseListener {
    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame("BB8 Says");
    private JPanel game;
    private JPanel menu;
    ImageIcon bbegif = new ImageIcon("tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
    public static final int WIDTH = 800, HEIGHT = 800;
    public int flashed = 0, glowTime, dark, ticks, indexPattern;
    public boolean creatingPattern = true;  
    public ArrayList<Integer> pattern;
    public Random random;
    private boolean gameOver;
    public int counter;
    public int temp;
    public int score;
    //these are the images, replace them if you like
    public ImageIcon bbb = new ImageIcon("bbb.gif");
    public JLabel bbbl = new JLabel(bbb);
    public ImageIcon bbbH = new ImageIcon("bbbH.gif");
    public ImageIcon bbg = new ImageIcon("bbg.gif");
    public JLabel bbgl = new JLabel(bbg);
    public ImageIcon bbgH = new ImageIcon("bbgH.gif");
    public ImageIcon bbgr = new ImageIcon("bbgr.gif");
    public JLabel bbgrl = new JLabel(bbgr);
    public ImageIcon bbgrH = new ImageIcon("bbgrH.gif");
    public ImageIcon bbo = new ImageIcon("bbor.gif");
    public JLabel bbol = new JLabel(bbo);
    public  ImageIcon bboH = new ImageIcon("bborH.gif");
    public  ImageIcon unhap = new ImageIcon("unhap.gif");
    public  JLabel unhapl = new JLabel(unhap);
    //these are the images, replace them if you like
    public BeeBee()
    {
        mainmenu();
    }
    public void start()
    {

    random = new Random();
    pattern = new ArrayList<Integer>();
    indexPattern = 0;
    dark = 2;
    flashed = 0;
    ticks = 0;
}
public void actionPerformed(ActionEvent e) 
{
    ticks++;

    if (ticks % 20 == 0)
    {
        flashed = 0;

        if (dark >= 0)
        {
            dark--;
        }}

    if (creatingPattern)
    {
        if (dark <= 0)
        {
            if (indexPattern >= pattern.size())
            {
                flashed = random.nextInt(40) % 4 + 1;
                pattern.add(flashed);
                indexPattern = 0;
                creatingPattern = false;
            }
            else
            {
                flashed = pattern.get(indexPattern);
                indexPattern++;
            }

            dark = 2;
        }}
    else if (indexPattern == pattern.size())
    {
        creatingPattern = true;
        indexPattern = 0;
        dark = 2;
    }}
public void mainmenu()
{
    Timer timer = new Timer(20, this);
    start();
    timer.start();
    frame.setSize(WIDTH +7, HEIGHT +30);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    menu = new JPanel();
    game = new JPanel();
    menu.setBackground(Color.yellow);
    game.setBackground(Color.yellow);
    JButton button = new JButton("Start");
    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            frame.setContentPane(game);
            frame.invalidate();
            frame.validate();
        };
    });

    menu.add(button);
    gamme();
    frame.add(menu);
    frame.setVisible(true);     
}

public void gamme()
{
    Border border = BorderFactory.createLineBorder(Color.black, 5);
    bbol.setBorder(border);
    bbgl.setBorder(border);
    bbgrl.setBorder(border);
    bbbl.setBorder(border);
    game.setLayout(new GridLayout(2,2));
    game.add(bbol);
    game.add(bbgl);
    game.add(bbgrl);
    game.add(bbbl);

}
public static void main(String[]args)
{
    new BeeBee();
}
@Override
public void mousePressed(MouseEvent e) 
{
    int x = e.getX(), y = e.getY();

    if (!creatingPattern && !gameOver)
    {
        if (x>0 && x<WIDTH/2 && y>0 && y<HEIGHT/2)
        {
            flashed = 1;
            ticks = 1;
            bbol.setIcon(bboH);
        }
        else if (x>WIDTH/2 && x<WIDTH && y>0 && y<HEIGHT/2)
        {
            flashed = 2;
            ticks = 1;
            bbgl.setIcon(bbgH);
        }
        else if (x>0 && x<WIDTH/2 && y>HEIGHT/2 && y<HEIGHT)
        {
            flashed = 3;
            ticks = 1;
            bbgrl.setIcon(bbgrH);
        }
        else if (x>WIDTH/2 && x<WIDTH && y>HEIGHT/2 && y<HEIGHT)
        {
            flashed = 4;
            ticks = 1;
            bbbl.setIcon(bbbH);
        }
        if (flashed != 0)
        {
            if (pattern.get(indexPattern)==flashed)
            {
                indexPattern++;
            }
            else
            {
                gameOver = true;
            }
        }
        else
        {
            start();
            gameOver = true;
        }}
    else if (gameOver)
    {
        start();
        gameOver = false;
    }
    if (flashed == 1) {
        bbol.setIcon(bboH);
    } else 
    {
        bbol.setIcon(bbo);
    }
 if (flashed == 2) {
        bbgl.setIcon(bbgH);
    } else 
    {
        bbgl.setIcon(bbg);
    }
 if (flashed == 3) {
        bbgrl.setIcon(bbgrH);
    } else 
    {
        bbgrl.setIcon(bbgr);
    }
 if (flashed == 4) {
        bbbl.setIcon(bbbH);
    } else 
    {
        bbbl.setIcon(bbb);
    }
    game.repaint();  
}
@Override
public void mouseClicked(MouseEvent e) 
{}
@Override
public void mouseEntered(MouseEvent e) 
{}
@Override
public void mouseExited(MouseEvent e) 
{}
@Override
public void mouseReleased(MouseEvent e) 
{}
导入java.awt.Color;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.util.ArrayList;
导入java.util.Random;
导入javax.swing.BorderFactory;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.border.border;
公共类BeeBee扩展JFrame实现ActionListener、MouseStener{
私有静态最终长serialVersionUID=1L;
私有JFrame=新JFrame(“BB8表示”);
私人游戏;
私人JPanel菜单;
ImageIcon bbegf=新的图像图标(“tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif”);
公共静态最终内部宽度=800,高度=800;
公共int flashed=0,发光时间,暗,滴答声,无模式;
public boolean creatingPattern=true;
公共阵列模式;
公共随机;
私有布尔gameOver;
公共int计数器;
公共内部温度;
公众智力得分;
//这些是图像,如果您愿意,请替换它们
公共图像图标bbb=新图像图标(“bbb.gif”);
公共JLabel bbbl=新JLabel(bbb);
公共图像图标bbbH=新图像图标(“bbbH.gif”);
公共图像图标bbg=新图像图标(“bbg.gif”);
公共JLabel bbgl=新JLabel(bbg);
公共图像图标bbgH=新图像图标(“bbgH.gif”);
公共图像图标bbgr=新图像图标(“bbgr.gif”);
公共JLabel bbgrl=新JLabel(bbgr);
公共图像图标bbgrH=新图像图标(“bbgrH.gif”);
公共图像图标bbo=新图像图标(“bbor.gif”);
公共JLabel bbol=新JLabel(bbo);
公共图像图标bboH=新图像图标(“bborH.gif”);
public ImageIcon unhap=新的ImageIcon(“unhap.gif”);
public JLabel unhapl=新JLabel(unhap);
//这些是图像,如果您愿意,请替换它们
公共蜜蜂()
{
主菜单();
}
公开作废开始()
{
随机=新随机();
pattern=newarraylist();
indexPattern=0;
黑暗=2;
闪烁=0;
滴答声=0;
}
已执行的公共无效操作(操作事件e)
{
ticks++;
如果(滴答声%20==0)
{
闪烁=0;
如果(暗>=0)
{
黑暗——;
}}
如果(创建模式)
{
if(暗=模式.size())
{
闪现=随机。下一个(40)%4+1;
模式。添加(闪现);
indexPattern=0;
creatingPattern=false;
}
其他的
{
flashed=pattern.get(indexPattern);
indexPattern++;
}
黑暗=2;
}}
else if(indexPattern==pattern.size())
{
creatingPattern=true;
indexPattern=0;
黑暗=2;
}}
公共void主菜单()
{
定时器=新定时器(20,此);
start();
timer.start();
框架尺寸设置(宽度+7,高度+30);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(false);
menu=newjpanel();
游戏=新JPanel();
菜单.背景(颜色.黄色);
游戏。挫折背景(颜色。黄色);
JButton按钮=新JButton(“开始”);
addActionListener(新建ActionListener()){
已执行的公共无效操作(操作事件e){
frame.setContentPane(游戏);
frame.invalidate();
frame.validate();
};
});
菜单。添加(按钮);
gamme();
frame.add(菜单);
frame.setVisible(true);
}
公共void gamme()
{
Border Border=BorderFactory.createLineBorder(Color.black,5);
bbol.订单(边界);
bbgl.订单(边界);
bbgrl.订单(边界);
bbbl.边境秩序;
游戏布局(新网格布局(2,2));
游戏添加(bbol);
game.add(bbgl);
游戏添加(bbgrl);
game.add(bbbl);
}
公共静态void main(字符串[]args)
{
新蜜蜂();
}
@凌驾
公共无效鼠标按下(MouseEvent e)
{
int x=e.getX(),y=e.getY();
如果(!creatingPattern&&!gameOver)
{
如果(x>0&&x0&&yWIDTH/2&&x0&&y0&&xHEIGHT/2&&yWIDTH/2&&xHEIGHT/2&&y
setIcon(…)
足以更改JLabel中的图像。我质疑您如何添加JLabel以及何时添加。如果您在程序运行期间添加组件,您必须调用
revalidate()
repaint()
在容器上,接受新JLabel的JPanel。通常最好在程序启动时添加JLabel,而不必担心此类问题。A

另外,什么事件触发了此更改?您的问题和代码不足以让我们确定这一点。我内心深处的某种想法让我担心,您还没有编写事件侦听器,因为我们没有看到上面任何代码表明上述代码存在于侦听器中。请澄清

如果仍然卡住,请创建并发布有效的


你仍然没有发布有效的,所以我们仍然要猜测(请阅读链接)

但是在你的MouseListener中,你正在改变闪现字段的状态,但是你没有调用任何会改变图像的代码。我怀疑你发布的关键代码是:

if (flashed == 1) 
{
     bbol.setIcon(bboH);
} else {
}
存在于您的GUI创建代码中。如果是,则只调用一次,并且只使用flashed的原始值。如果更改flashed字段的值,则不会神奇地重新调用此代码,而是您必须自己调用它。。换句话说,此代码应位于鼠标侦听器中

同样,如果你需要更多的帮助,请发一个有效的代码给她
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class BlinkingIcon extends JPanel {
    private static final int IMG_WIDTH = 200;
    private static final int GAP = 8;
    private static final int TIMER_DELAY = 100;

    // index into icons array
    private int iconIndex = 0;

    // array of image icons
    private Icon[] icons = new Icon[2];

    // JLabel that displays the icons
    private JLabel mainLabel = new JLabel();

    // Swing Timer that when started swaps the icons 
    private Timer flashingTimer = new Timer(TIMER_DELAY, new TimerListener());

    public BlinkingIcon() {
        // fill icons array with icons
        icons[0] = createIcon(Color.WHITE);
        icons[1] = createIcon(Color.RED);

        // add the first icon to the JLabel
        mainLabel.setIcon(icons[iconIndex]);

        // add the JLabel to the main JPanel, the GUI
        add(mainLabel);

        // add a MouseListener to the JLabel
        // one that turns the flashing timer on and off
        mainLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (flashingTimer.isRunning()) {
                    flashingTimer.stop();
                } else {
                    flashingTimer.start();
                }
            }
        });
    }

    // for this example, I will create a simple icon that's little more than
    // a color circle, but any icons would work
    private Icon createIcon(Color white) {
        BufferedImage img = new BufferedImage(IMG_WIDTH, IMG_WIDTH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(white);
        g2.fillOval(GAP, GAP, IMG_WIDTH - 2 * GAP, IMG_WIDTH - 2 * GAP);
        g2.setStroke(new BasicStroke((float) GAP));
        g2.setColor(Color.BLACK);
        g2.drawOval(GAP, GAP, IMG_WIDTH - 2 * GAP, IMG_WIDTH - 2 * GAP);
        g2.dispose();
        Icon icon = new ImageIcon(img);        
        return icon;
    }

    // ActionListener for our flashingTimer
    private class TimerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // add one to the iconIndex, the index to the icons array
            iconIndex++;
            iconIndex %= icons.length; // set to 0 if it reaches length of array
            mainLabel.setIcon(icons[iconIndex]); // swap the icon!            
        }
    }

    private static void createAndShowGui() {
        BlinkingIcon mainPanel = new BlinkingIcon();

        JFrame frame = new JFrame("Blinking Icon");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
game.addMouseListener(this);