在GUI中交换2d按钮数组 - Java

时间:2015-03-14 18:10:45

标签: java swing jbutton

我正试图找到一种方法来切换按钮或在2D按钮阵列中交换按钮图标。我正在尝试创建一个棋盘游戏并移动棋子。

我不确定我的逻辑是否完全在这里,但我的过程是...创建一个扩展JButton的Gamepiece类。将这些按钮传递到2d数组并将该按钮添加到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();
   }
}

1 个答案:

答案 0 :(得分:2)

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

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