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

标签 java swing button

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

相关文章:

java - 为什么添加这 3 行可以防止 NullPointerException?

java - 将图像文件设置为背景

java - 为什么 HashMap<String, Object> 不接受 HashMap<String, List> 实例?

java - 无法在java中重新绘制

java - Groovy 的 Java HTTPBuilder?

java - JDesktopPane 的背景设置

java - 从 JFileChooser 获取 file.properties 并在 ResourceBundle 上使用它

javascript - 如何用一个按钮改变其他按钮的样式?

html - 使用垫子按钮以 Angular 构建导航栏

css - 如何在 JavaFX 中的按钮上使用 CSS(见下图)实现 "shine"效果?