Java 如何为国际象棋游戏重新绘制JButtons并更新GUI
我想下一盘棋,但我不知道如何顺利地重新绘制我的单元格。我希望每次一个片段移动时都重新绘制它们。我试图继续添加Jframes,但这会使它回到旧的框架。有没有简单的方法来更新GUI的某些部分Java 如何为国际象棋游戏重新绘制JButtons并更新GUI,java,swing,user-interface,jframe,jbutton,Java,Swing,User Interface,Jframe,Jbutton,我想下一盘棋,但我不知道如何顺利地重新绘制我的单元格。我希望每次一个片段移动时都重新绘制它们。我试图继续添加Jframes,但这会使它回到旧的框架。有没有简单的方法来更新GUI的某些部分 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class GUI { public stat
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI {
public static JFrame frame; //global variable for drawn frame
public static ChessBoardPane tiles;
public GUI(final Piece[][] board) { //GUI for Board and Pieces
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
frame = new JFrame("Chess"); //initialize frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes on exit
drawFrame(board); //draw frame
}
});
}
public void drawFrame(final Piece[][] board){ //updates gui with a new frame
try{
tiles=new ChessBoardPane(board);
frame.add(tiles); //add chess board to frame
frame.pack(); //resizes frame to fit chess grid
frame.setLocationRelativeTo(null); //window in middle of screen
frame.setVisible(true); //visible
} catch(Exception ex){
}
}
public class ChessBoardPane extends JPanel{ //creates a grid of jButtons
public ChessBoardPane(Piece board[][]) {
int index = 0;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for (int row = 0; row < Board.width; row++) {
for (int col = 0; col < Board.height; col++) {
Color color = index % 2 == 0 ? Color.LIGHT_GRAY : Color.DARK_GRAY; //cells are dark and light gray
gbc.gridx = col;
gbc.gridy = row;
String symbol;
if(board[col][Board.height-row-1]!=null){
symbol=board[col][Board.height-row-1].symbol; //chooses the correct picture for a piece
}
else
symbol=null;
Cell tile = new Cell(color,symbol);
tile.addActionListener(new ButtonActionListener(col, Board.height-row-1));
add(tile, gbc); //add cell to gui
index++;
}
index++;
}
}
private class ButtonActionListener implements ActionListener{ //used to get x and y coordinates for button presses and call methods
private int x;
private int y;
public ButtonActionListener(int x, int y){
this.x=x;
this.y=y;
}
public void actionPerformed(ActionEvent e){ //runs commands when a tile is pressed
Game.attemptMove(x,y);
}
}
}
public class Cell extends JButton { //sets up cell behavior
int x;
int y;
public Cell(Color background, String symbol) {
setContentAreaFilled(false);
if(symbol!=null){
ImageIcon img = new ImageIcon(symbol); //puts picture in cell
setIcon(img);
}
setBorderPainted(false);
setBackground(background); //sets cell color
setOpaque(true);
}
@Override
public Dimension getPreferredSize() { //sets size of cell
return new Dimension(75, 75);
}
}
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
公共类GUI{
公共静态JFrame框架;//绘制框架的全局变量
公共静态棋盘格;
公共图形用户界面(最后一块[]电路板){//GUI用于电路板和电路板
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(例外情况除外){
}
frame=newjframe(“Chess”);//初始化帧
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出时关闭
画框(板);//画框
}
});
}
public void drawFrame(最后一块[][]板){//使用新框架更新gui
试一试{
瓷砖=新棋盘格(棋盘);
frame.add(tiles);//将棋盘添加到框架
frame.pack();//调整框架大小以适应棋盘格
frame.setLocationRelativeTo(null);//屏幕中间的窗口
frame.setVisible(true);//可见
}捕获(例外情况除外){
}
}
公共类ChessBoardPane扩展了JPanel{//创建一个JButton网格
公共棋盘格(棋子棋盘[]){
int指数=0;
setLayout(新的GridBagLayout());
GridBagConstraints gbc=新的GridBagConstraints();
用于(int row=0;row
另请参见此图。如果GUI使用按钮,则“不知道如何平滑地重新绘制单元格”,只需为按钮设置一个新图标(如上面的示例)。一般提示:1)为了更快地获得更好的帮助,发布一个(最小完整可验证的示例)。2) 在源代码中只需要一行空白就可以了。{
之后或}
之前的空行通常也是多余的。请在电路板和按钮之间创建映射,以便每个按钮与电路板具有一对一的关系。当电路板更新时,每个按钮(或至少涉及交换的两个按钮)都应相应更新。不要一直添加和删除内容,不要重建帧,不要显示多个帧。。。