无法更改ActionListener的源代码

时间:2016-11-23 22:21:03

标签: java jframe jpanel actionlistener

好的,所以我觉得这只是我在某些时候错过的愚蠢行为,但我已经坚持了一段时间而无法找到解决方案。我必须为我正在参加的Java课程创建一个名为Tsuro的游戏。我通过在JFrame上添加一个带有6x6网格的JPanel来创建棋盘并存储了TsuroButtons(这是我教授为该项目提供的一个类,它是在游戏的磁贴上创建图形的东西)。我通过一个数组将TsuroButtons放在网格中的每个tile上。我在电路板上的每个瓷砖上放了一个ActionListener。现在,真正的问题是在我的代码底部的ActionPerformed方法中。第一部分只是让我突出显示每个玩家手中每个牌的方式(一个单独的JFrame,其设置方式与棋盘相同,但包含我将尝试移动到棋盘上的牌)。第二位是我遇到麻烦的地方。

public void actionPerformed(java.awt.event.ActionEvent e) {
  TsuroButton b = (TsuroButton)e.getSource();
  if(e.getActionCommand() == "Hand") {
    save.setBackground(Color.white);
    b.setBackground(Color.yellow);
    save = b;
  }
  else if(e.getActionCommand() == "Board") {
     b = save;
  } 
}

我正在尝试将b的值(事件的来源/被点击的图块)更改为save的值,该值是存储在方法外部的字段。

TsuroButton save = new TsuroButton();

但是,每当我打开JFrame并尝试将每个磁贴从播放器手JFrame上的位置交换到在Board JFrame上单击的位置时,都不会发生任何事情。我认为这与JPanel有关,因为我尝试通过将已经包含在JPanel中的TsuroButton的值更改为具有不同背景的不同TsuroButton来更改我在测试文档中创建的JPanel的背景,以及虽然它在技术上仍然改变了它,但实际显示的JPanel没有变化。

那么,简而言之,您如何在ActionListener中更改源的值?对不起,我做了一个很糟糕的解释这个,但我真的想不出一个更好的解释方法,而不仅仅是把所有与问题相关的代码放在这里,这不完全合理。但是,这是构造函数。

public Tsuro(int row, int column, int handsize) {
    JFrame boardFrame = new JFrame();
    boardFrame.setSize(1000, 1000);
    boardFrame.setTitle("Tsuro");
    JPanel board = new JPanel(new GridLayout(row, column));
    boardFrame.add(board);
    TsuroButton[][] tsuroArray = new TsuroButton[row][column];

    /* This loop stores a TsuroButton at each point in the array, adds an ActionListener to them and adds them to the
     * board.
     * Precondition: There must be a JPanel to add the array to and an array to store the TsuroButtons and 
     * ActionListeners in.
     * Subgoal: To store a TsuroButton and ActionListener in each part of the array and add that part to the board
     * JPanel.
     * */
    int horizIndex = 0;
    int vertIndex = 0;
    int total = 0;
    while(total < (row * column)) {
      board.add(tsuroArray[horizIndex][vertIndex] = new TsuroButton());
      tsuroArray[horizIndex][vertIndex].addActionListener(this);
      tsuroArray[horizIndex][vertIndex].setActionCommand("Board");

      horizIndex++;
      total++;
      if(horizIndex > row - 1) {
        vertIndex++;
        horizIndex = 0;
      }
    }

    boardFrame.setVisible(true);

    JFrame player1Hand = new JFrame();
    JFrame player2Hand = new JFrame();
    player1Hand.setSize(500, 200);
    player2Hand.setSize(500, 200);
    JPanel player1Panel = new JPanel(new GridLayout(1, handsize));
    JPanel player2Panel = new JPanel(new GridLayout(1, handsize));
    player1Hand.add(player1Panel);
    player2Hand.add(player2Panel);

    TsuroButton[] player1Array = new TsuroButton[handsize];
    TsuroButton[] player2Array = new TsuroButton[handsize];

    /* The purpose of this loops is to store a TsuroButton at each section in the array, set random connections on
     * those TsuroButtons, to add a stone to the TsuroButtons, and to add an ActionListener.
     * Preconditions: There must be an array for the player 1 and 2 hands as well as a JPanel to add each point in the
     * array to.
     * Subgoal: To add the completed TsuroButtons to the JPanel and repeat until finished.
     * */
    int index = 0;
    while(index < handsize) {
      player1Panel.add(player1Array[index] = new TsuroButton());
      player2Panel.add(player2Array[index] = new TsuroButton());
      player1Array[index].setConnections(TsuroButton.makeRandomConnectArray());
      player2Array[index].setConnections(TsuroButton.makeRandomConnectArray());
      player1Array[index].addStone(Color.BLUE, 6);
      player2Array[index].addStone(Color.GREEN, 2);
      player1Array[index].addActionListener(this);
      player2Array[index].addActionListener(this);
      player1Array[index].setActionCommand("Hand");
      player2Array[index].setActionCommand("Hand");
      index++;
    }

    player1Hand.setVisible(true);
    player2Hand.setVisible(true);

    try {
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch (Exception e) {
    }
}

0 个答案:

没有答案