[英]JFrame not refreshing with repaint
I have a visual class Gameboard
that consists of a frame, representing a 'board'. 我有一个视觉类的
Gameboard
,它由一个框架组成,代表一个“木板”。 The idea is that when a button is clicked the players on the board will move, and so when the button is clicked the board must refresh. 这个想法是,单击按钮时,棋盘上的玩家将移动,因此,单击按钮时,棋盘必须刷新。 This is the frame:
这是框架:
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);
}
}
The newGame
method is working correctly but when the button tries to repaint the frame nothing happens. newGame
方法正常工作,但是当按钮尝试重新绘制框架时,什么也没有发生。 Specifically the new Game
button. 特别是
new Game
按钮。
The frame consists of a grid, each of which are populated with one a three types of cells, each extending the jpanel class. 框架由一个网格组成,每个网格中都填充了三种类型的单元格中的一种,每种单元格都扩展了jpanel类。
The repaint is being called in the JButton "new game". 重新绘制在JButton“新游戏”中被调用。 The Cells are added in a loop fashion.
单元以循环方式添加。
Any help is welcome! 欢迎任何帮助!
Instead of simply calling invalidate()
and repaint()
from the JPanel
level, call them from the JFrame
level. 与其从
JPanel
级别简单地调用invalidate()
和repaint()
, JPanel
从JFrame
级别调用它们。 Like so: 像这样:
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();
}
});
If anyone has a similar issue this is how I fixed it: 如果有人遇到类似问题,这就是我的解决方法:
Rather than repainting the JFrame, I simply disposed of the the current frame and created a new one with the updated positions. 我没有重绘JFrame,而是简单地处理了当前框架并创建了一个具有更新位置的新框架。 Its a bit of a 'Gaffer-tape' fix but it work how I want it to.
它有点像“ Gaffer-tape”修复程序,但是按我希望的那样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.