gpt4 book ai didi

java - JFrame 重绘后不刷新

转载 作者:太空宇宙 更新时间:2023-11-04 13:11:24 24 4
gpt4 key购买 nike

我有一个视觉类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 方法工作正常,但当按钮尝试重新绘制框架时,没有任何反应。特别是新游戏按钮。

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

重绘在 JButton“新游戏”中被调用。单元格以循环方式添加。

欢迎任何帮助!

最佳答案

如果有人有类似的问题,这就是我解决它的方法:

我没有重新绘制 JFrame,而是简单地处理当前帧并使用更新的位置创建一个新帧。它有点像“Gaffer-tape”修复,但它按照我想要的方式工作。

关于java - JFrame 重绘后不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33921810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com