gpt4 book ai didi

java - 在 GUI 中交换按钮的二维数组 - Java

转载 作者:行者123 更新时间:2023-12-02 04:52:53 25 4
gpt4 key购买 nike

我正在尝试找到一种方法来切换按钮或交换二维按钮数组中的按钮图标。我正在尝试创建一个棋盘游戏并移动棋子。

我不确定我的逻辑是否完全偏离这里,但我的思考过程是.. 创建一个扩展 JButton 的 Gamepiece 类。将这些按钮传递到二维数组中并将该按钮添加到 GridLayout。

现在,我觉得交换按钮上的图标更简单,但我似乎不知道如何正确地做到这一点。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Board implements ActionListener {
private int p1;
private int p2;
private int fromRow;
private int fromCol;
private int toRow;
private int toCol;
private JPanel grid = new JPanel(new GridLayout(8,8));
private JFrame jf = new JFrame();
GamePieces gp;
private boolean isFirstClick = true;
Icon eagles = new ImageIcon("eagles.png");
Icon cowboys = new ImageIcon("cowboys.png");
GamePieces boardGame [][] = new GamePieces [8][8];
int board [][] = new int [][]{{1,1,1,1,0,0,0,0},
{1,1,1,0,0,0,0,0},
{1,1,0,0,0,0,0,0},
{1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,2},
{0,0,0,0,0,0,2,2},
{0,0,0,0,0,2,2,2},
{0,0,0,0,2,2,2,2}};
///////////////////////////////////////////////////////////////////
public Board(){
jf.setTitle("Board Game");
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(640,640);
JMenuBar menuBar = new JMenuBar();
jf.setJMenuBar(menuBar);
JMenu file = new JMenu("File");
JMenu about = new JMenu("About");
menuBar.add(file);
menuBar.add(about);

createBoard();
jf.setVisible(true);
}// end constructor
////////////////////////////////////////////////////////////////////
public void movePiece(){

for(int row = 0; row < boardGame.length; row++){
for(int col = 0; col < boardGame.length; col++){
boardGame[toRow][toCol] = boardGame[fromRow][fromCol];
gp = new GamePieces(fromRow,fromCol);
gp.setIcon(eagles);
boardGame[toRow][toCol] = gp;
jf.repaint();
}
}
}
/////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent ae){
for(int row = 0; row < boardGame.length; row++){
for(int col = 0; col < boardGame.length; col++){
if(ae.getSource() == boardGame[row][col]){
if(isFirstClick){
fromRow = boardGame[row][col].getRow();
fromCol = boardGame[row][col].getCol();
isFirstClick = false;
System.out.println("First Row " + boardGame[row][col].getRow() + " Col " + boardGame[row][col].getCol());
}
else {
toRow = boardGame[row][col].getRow();
toCol = boardGame[row][col].getCol();
System.out.println("Second Row " + boardGame[row][col].getRow() + " Col " + boardGame[row][col].getCol());
this.movePiece();
}
}
}
}
}
///////////////////////////////////////////////////////
public void createBoard(){
for(int row = 0; row < board.length; row++){
for(int col = 0; col < board.length; col++){
if (board[row][col] == 1){
gp = new GamePieces(row,col);
gp.setIcon(eagles);
boardGame[row][col] = gp;
grid.add(boardGame[row][col]);
boardGame[row][col].addActionListener(this);
}
else if (board[row][col] == 0){
gp = new GamePieces(row,col);
boardGame[row][col] = gp;
grid.add(boardGame[row][col]);
boardGame[row][col].addActionListener(this);
}

else if(board[row][col] == 2){
gp = new GamePieces(row,col);
gp.setIcon(cowboys);
boardGame[row][col] = gp;
grid.add(boardGame[row][col]);
boardGame[row][col].addActionListener(this);
}
}
}

jf.add(grid);

}



class GamePieces extends JButton {
private int row;
private int col;
private String player;

public GamePieces(int row, int col){
this.row = row;
this.col = col;
}

public int getRow(){
return row;
}

public int getCol(){
return col;
}

public String getPlayer(){
return player;
}

}

public static void main(String [] args){
Board Halmas = new Board();
}
}

最佳答案

不,不要交换按钮,因为没有必要,而且这样做没有任何好处。相反,请使用 MVC 或模型- View -控制程序结构,并根据模型状态的变化交换 JButton 或 JLabels(我的偏好)所持有的图标。

无论您使用哪种整体技术,请交换图标,而不是按钮。

关于java - 在 GUI 中交换按钮的二维数组 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29052403/

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