我有' n'根据用户的决定,按钮数量。每当我点击我想要的按钮时,它会给我点击它的次数。棘手的是我没有为按钮设置任何名称,用户就是这样做的。所以我的问题是,我如何检查' countAmount'使用e.getActionCommand()是1还是更多?
我真的坚持这个问题,如果有人能提供帮助那就太棒了!这是我的计划的一部分,任何帮助都会很棒!
private static int countAmount = 0;
public Example()
{
str = JOptionPane.showInputDialog("What is the name of the new button?");
JButton b18 = new JButton(str);
//The actionlistener stuff
countAmount++;
if (countAmount % 2 != 0)
{
System.out.println(e.getActionCommand() + "was clicked, count is even");
}
else
{
System.out.println("The button was clicked, count is odd");
}
if (countAmount.(e.getActionCommand) == 1)
{
System.out.println("This button has been clicked " + countAmount + " times);
}
}
答案 0 :(得分:2)
ArrayList<JButton>
。getSource()
来获取对实际按钮本身的引用。ArrayList<Integer>
HashMap<JButton, Integer>
,然后只需递增与源JButton键关联的Integer值。