更改JPanel后JFrame无法更新

时间:2018-07-09 23:19:00

标签: java swing jframe jpanel

我试图制作一些有趣的小游戏,制作完主菜单后,我想更改JPanel。我尝试过,但是我的JFrame保持冻结状态,一直停留在最后一帧,然后才更改JPanel! 到目前为止,这是我的代码! (代码简化)

public class Window extends JFrame{

private static final long serialVersionUID = -7797736947468761815L;
private JPanel panel;
private final int buttonX = 440, buttonY = 400;
private final Font buttonFont = new Font(Main.BORING_FONT.getFontName(), Font.PLAIN, 50);

public Window() {
    this.setSize(new Dimension(1080, 720));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    loadMainMenu();
    this.getContentPane().add(panel);
    this.setVisible(true);
}
public void loadMainMenu() {
    panel = new JPanel() {
        private static final long serialVersionUID = -2969332078282980016L;
        @Override
        public void paintComponent(Graphics g) {
            g.drawImage(Main.MENU_BACKGROUND, 0, 0, this);
            if(something){                  
                g.drawImage(Main.BUTTON, buttonX, buttonY, this);
            } else {
                g.drawImage(Main.BUTTON_HOVER, buttonX, buttonY, this);                 
            }
        }
    };
    panel.setPreferredSize(this.getSize());
}
public void loadGame() {
    JPanel panel = new JPanel() {
        private static final long serialVersionUID = -5378871926271288617L;
        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.red);
            g.fillRect(0, 0, 1080, 720);
            g.drawImage(Main.CHARACTER, 540, 360, this);
        }
    };
    panel.setPreferredSize(this.getSize());
    this.removeAll();
    this.add(panel);
}

}

0 个答案:

没有答案