JFrame没有重新刷新

时间:2015-11-25 16:36:13

标签: java swing jframe repaint

我有一个视觉类Gameboard,它由一个框架组成,代表一个“板”。这个想法是当点击一个按钮时,棋盘上的玩家会移动,所以当点击按钮时,棋盘必须刷新。这是框架:

public Gameboard(Game game, int size, ArrayList<Enemy> e, Ogre o){

    this.ogre = o.getPosition();
    if(this.enemies.isEmpty() == false){
        this.enemies.clear();
    }
    if(e.isEmpty() == false){
        for(Enemy en : e){
            this.enemies.add(en.getPosition());
        }
    }
    this.game = game;

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Swamp Escape!");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new GridPane(size));
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            frame.setResizable(false);

        }
    });
}

public class GridPane extends JPanel {

    public GridPane(int size) {

        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                gbc.gridx = col;
                gbc.gridy = row;

                if(col == ogre.getX() && row == ogre.getY()) {
                    OgrePane ogre = new OgrePane();
                    Border border = null;
                    if (row < size - 1) {
                        if (col < size - 1) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                        }
                    } else {
                        if (col < size - 1) {
                            border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }
                    ogre.setBorder(border);
                    add(ogre, gbc);
                } else {
                    CellPane cellPane = new CellPane();
                    Border border = null;
                    if (row < size - 1) {
                        if (col < size - 1) {
                            border = new MatteBorder(1, 1, 0, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 0, 1, Color.GRAY);
                        }
                    } else {
                        if (col < size - 1) {
                           border = new MatteBorder(1, 1, 1, 0, Color.GRAY);
                        } else {
                            border = new MatteBorder(1, 1, 1, 1, Color.GRAY);
                        }
                    }
                    cellPane.setBorder(border);
                    add(cellPane, gbc);
                }

            }
        }

        JButton newGame = new JButton("New Game");
        newGame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.newGame();
                ogre = game.getHek().getPosition();
                if(enemies != null) enemies.clear();
                for(Enemy en : game.enemies){
                    enemies.add(en.getPosition());
                }
                revalidate();
                repaint();
            }
        });
        gbc.gridx = 0;
        gbc.gridy = size + 1;
        add(newGame, gbc);

        JButton move = new JButton("Next Move");
        move.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.nextMove();
            }
        });
        gbc.gridx = 1;
        gbc.gridy = size + 1;
        add(move, gbc);

        JButton undo = new JButton("Undo");
        gbc.gridx = 2;
        gbc.gridy = size + 1;
        add(undo, gbc);

        JButton diet = new JButton("Change Diet");
        gbc.gridx = 3;
        gbc.gridy = size + 1;
        add(diet, gbc);
    }
}

newGame方法工作正常,但当按钮尝试重新绘制框架时,没有任何反应。特别是new Game按钮。

框架由一个网格组成,每个网格都填充一个三种类型的单元格,每个单元格都扩展了jpanel类。

重拍是在JButton“新游戏”中调用的。细胞以循环方式添加。

欢迎任何帮助!

2 个答案:

答案 0 :(得分:0)

不是简单地从invalidate()级别调用repaint()JPanel,而是从JFrame级别调用它们。像这样:

JButton newGame = new JButton("New Game");
    newGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            game.newGame();
            ogre = game.getHek().getPosition();
            if(enemies != null) enemies.clear();

            for(Enemy en : game.enemies){
                enemies.add(en.getPosition());
            }

            JFrame frame = (JFrame) getRootPane().getParent();
            frame.pack();
        }
    });

答案 1 :(得分:0)

如果有人有类似的问题,我就是这样修理的:

我不是重新绘制JFrame,而是简单地处理当前帧并创建一个具有更新位置的新帧。它有点像Gaffer-tape&#39;修复,但它的工作方式我想要它。