[英]Swapping 2d array of buttons in GUI - Java
I am trying to find a way to either SWITCH the buttons or SWAP the button icons in a 2D array of buttons. 我正在尝试找到一种方法来切换2D按钮阵列中的按钮或交换按钮图标。 I am attempting to create a board game and move pieces around. 我正在尝试创建棋盘游戏并四处移动。
I'm not sure if my logic is just completely off here but my though process is.. Create a Gamepiece class that extends JButton. 我不确定我的逻辑是否在这里完全关闭,但是我的流程是..创建一个扩展JButton的Gamepiece类。 Pass those buttons into a 2d array and add that button to the GridLayout. 将这些按钮传递到2d数组中,然后将该按钮添加到GridLayout中。
Now, I feel like swapping the icons on the buttons is simpler but I cant seem to figure out how to do it correctly. 现在,我觉得交换按钮上的图标更简单,但是我似乎无法弄清楚如何正确地进行操作。
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();
}
}
No, don't swap buttons as there's no need, and no benefit to doing this. 不,不要交换按钮,因为这样做没有必要,也没有好处。 Instead use an MVC or Model-View-Control program structure and swap Icons held by JButtons or JLabels (my preference) as dictated by changes in the model's state. 取而代之的是使用MVC或Model-View-Control程序结构,并根据模型状态的变化来交换由JButtons或JLabels(我的偏爱)持有的图标。
Regardless of what overall technique you use, swap your icons, not your buttons. 无论您使用什么整体技术,都可以交换图标而不是按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.