java - 将 JButton 放入堆栈中

标签 java null stack jbutton undo

我正在制作一个统计程序来练习我的 Java GUI 技能。

我有一个程序,通过点击篮球运动员名字下的 JButton 来记录篮球运动员的统计数据。然后,它将统计数据添加到运行总计中并更新记分板。

现在是我创建撤消按钮的时候了。

因此,每次执行操作时,我都会将源按钮添加到 JButton 堆栈中。涉及到一些转换,所以最终结果是这样的:

JButton source = (JButton) e.getSource();
theStack.push(source);

稍后,在 actionPerformed 方法中,我尝试通过撤消函数调用:

if(source.getText().equals("Undo")){
    System.out.println("Undo");
    JButton last = this.theStack.pop();
    System.out.println(last.getText()); //Works fine.
    System.out.println(last.getName()); //Produces a null value.
    int player = Integer.parseInt(last.getName().trim());
    undo(player, last.getText(), activePlayers);
}

为什么我得到的名称为空。当 Eclipse 尝试将名称转换为 int 时,它会抛出异常,因为它正在转换 null 值。我在 actionPerformed 的其他部分使用了 .getName(),但在这里没有使用?

我的名称设置代码,在 for 循环中完成了很多次。

output[i][j] = new JButton("Make Two Points");
output[i][j].setName(i + "");

最简单形式的问题。

public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        ArrayList<Integer> activePlayers = new ArrayList<Integer>();
        activePlayers.add(player0Select.getSelectedIndex());
        activePlayers.add(player1Select.getSelectedIndex());
        activePlayers.add(player2Select.getSelectedIndex());
        activePlayers.add(player3Select.getSelectedIndex());
        activePlayers.add(player4Select.getSelectedIndex());

        JButton source = (JButton) e.getSource();
        theStack.push(source);

        if(source.getText().equals("Make Two Points")){
            this.makeTwoPoints(source.getName(), activePlayers); //source.getName() works here.
            System.out.println("Two Points");
        }
        if(source.getText().equals("Undo")){
            System.out.println("Undo");
            JButton last = this.theStack.pop();
            System.out.println(last.getText());
            System.out.println(last.getName()); //last.getName() produces null here.
            int player = Integer.parseInt(last.getName().trim());
            undo(player, last.getText(), activePlayers);
        }
}

最佳答案

因为您从未设置过JButton名称,也不应该设置。每个组件都有一个名称属性,可以通过 setName(...) 设置方法,如果从未调用 setter 方法,则该名称为 null。但这个属性有什么意义呢?这里就不多说了。

如果这是我的项目,我不会堆叠 JButton,而是会堆叠模型对象,或者可能是控件(操作)。我们不要将我们的模型与我们的观点混为一谈。

<小时/>

编辑

举一个简单的例子来说明我的意思,

您可以有一个 StatAction 枚举,其中包含您的三个(或更多统计操作),例如,

public enum StatAction {
   MAKE_2_PTS("Make Two Points"), MISS_2_PTS("Miss Two Points"), 
   MAKE_3_PTS("Make Three Points");

   private String text;

   private StatAction(String text) {
      this.text = text;
   }

   @Override
   public String toString() {
     return text;
   }

   public String getText() {
      return text;
   }

}

您可以有一个 Player 类,其中可以包含名称字段以及 List<StatAction> ,例如它可能有...

public class Player {
   private String name;
   private List<StatAction> statActionList = new ArrayList<>();

   // ....

   public String getName() {
      return name;
   }

   public void addStatAction(StatAction statAction) {
      statActionList.add(statAction);
   }

   public void removeStatAction(StatAction statAction) {
      statActionList.remove(statAction);
   }

   public void removeLastStatAction() {
      if (statActionList.size() > 0) {
         statActionList.remove(statActionList.size() - 1);
      }
   }

   //.....

}

然后撤消可以从玩家列表中删除最后一个 StatAction。然后,统计信息的显示可以通过监听器动态更改。

关于java - 将 JButton 放入堆栈中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21618075/

相关文章:

c - c 中函数不能返回 null

java - 使用 find-min/find-max 堆栈比 O(n) 更有效?

java - 调用 notifyDataSetChanged() 后丢失 EditText 值;

java - 找到多个 Java 集合中最常见的术语?

java - Spring JPA - 数据完整性关系

c++ - 堆栈.h :13:3: error: ‘Cell’ does not name a type

java - 非原子缓存

php - 在 PHP 中避免空值时,我可以重新实现 isset 吗?

java - 如何通过 Java 中的条件锁定实例化?

c - fprintf 和 NULL 终止符