繁体   English   中英

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

[英]Swapping 2d array of buttons in GUI - Java

我正在尝试找到一种方法来切换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();
   }
}

不,不要交换按钮,因为这样做没有必要,也没有好处。 取而代之的是使用MVC或Model-View-Control程序结构,并根据模型状态的变化来交换由JButtons或JLabels(我的偏爱)持有的图标。

无论您使用什么整体技术,都可以交换图标而不是按钮。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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