我有一个视觉类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“新游戏”中被调用。单元以循环方式添加。
欢迎任何帮助!
最佳答案
如果有人遇到类似问题,这就是我的解决方法:
我没有重绘JFrame,而是简单地处理了当前框架,并使用更新的位置创建了一个新框架。它有点像“ Gaffer-tape”修复程序,但是它可以按照我的意愿来工作。