我应该在哪里宣布我的"计数"可以正确吗?

时间:2016-02-25 03:44:46

标签: java swing button

我想知道我应该在哪里正确放置我的countA,因为我希望将计数添加到每个按钮中。 '计数'开头的变量是制作新按钮,' 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);
                        }
                    } 
                }

1 个答案:

答案 0 :(得分:2)

  

如何将其作为ActionListener的实例字段?

就像你对任何其他班级一样......

public static void main(String[] args) {
    Properties p1 = new Properties();
    OutputStream os1 = null;

    try {
        os1 = new FileOutputStream("xanadu123.properties");

        //set the properties value
        p1.setProperty("database", "localhost");
        p1.setProperty("1", "one");
        p1.setProperty("2", "two");

        p1.store(os1, "this is the comment");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (os1 != null) {
            try {
                os1.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }   
    }