我想知道应该在哪里正确放置 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);
}
}
}
最佳答案
how would I make it an instance field of the ActionListener?
就像您在任何其他类(class)中一样......
class ClickListener implements ActionListener
{
protected int countA;
public void actionPerformed(ActionEvent e)
{
count++;
关于java - 我应该在哪里正确声明我的 "count"可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617841/