我想知道应该将countA正确放置在哪里,因为我希望将计数添加到所创建的每个按钮中。开头的'count'变量用于创建新按钮,'countA'是我要声明的计数,但未编译。这是我的代码段:同样,我的问题是,我应该在哪里声明countA变量,以便它每次为按钮创建一个新的计数。谢谢!
public static int count = 0;
class ClickListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
count++;
if (count % 2 != 0)
{
str = JOptionPane.showInputDialog("What is the name of the new Button?");
JButton b = new JButton(str);
b18.setPreferredSize(new Dimension(125, 25));
int countA = 0;
b.setBackground(Color.BLUE);
b.setContentAreaFilled(false);
b.setOpaque(true);
b.setFocusable(false);
add(b18);
class ClickListenerEighteen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
countA++;
if (countA % 2 != 0)
{
System.out.println("This new button was clicked")
}
else
{
System.out.println("This button was clicked so that I know");
}
}
}
ActionListener bClicked = new ClickListener();
b.addActionListener(bClicked);
}
}
}
如何将其设为ActionListener的实例字段?
就像您上任何其他课程一样...
class ClickListener implements ActionListener
{
protected int countA;
public void actionPerformed(ActionEvent e)
{
count++;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句