我正在制作一个统计程序来练习我在Java中的GUI技能。将JButton放在堆栈上
我有一个程序,通过按下他们的名字打一个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
时发生异常,因为它正在转换空值。我在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);
}
}
当我生成它们时,我确实设置了它们,该名称对应于列号,它将其转换为我的程序中的活动播放器。 –
@TrevorHutto:JVM正在告诉你,否则我相信JVM。请显示您的设置代码。 –
@TrevorHutto:您需要创建并发布可测试[mcve](http://stackoverflow.com/help/mcve),以便我们能够充分理解您的问题。 –