简体   繁体   中英

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. 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. Pass those buttons into a 2d array and add that button to the 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.

Regardless of what overall technique you use, swap your icons, not your buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM