gpt4 book ai didi

java - 我应该在哪里正确声明我的 "count"可用?

转载 作者:行者123 更新时间:2023-12-02 03:54:27 26 4
gpt4 key购买 nike

我想知道应该在哪里正确放置 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com