让JLabel可以访问静态变量吗?

时间:2014-01-26 05:27:25

标签: java jframe jpanel jlabel

我正在玩JFrames以获得乐趣,并且无法让面板显示静态变量。我很感激任何帮助。这是我正在使用的代码:

import java.awt.event.*;
import javax.swing.*;

public class JButtonTester
{
    static int counter = 0;
    public static void main(String[]args)
    {
        class ClickCounter implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                counter++;
                System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment");
            }
        }
        class ClickDecrement implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                counter--;
                System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment");
            }
        }
        JFrame firstFrame = new JFrame();
        JLabel counter = new JLabel("Count: " + counter);
        JPanel firstPanel = new JPanel();

        JButton firstButton = new JButton("Click me to increase your count!");
        firstPanel.add(firstButton);
        ActionListener firstListener = new ClickCounter();
        firstButton.addActionListener(firstListener);

        JButton secondButton = new JButton("Click me to decrease your count!");
        firstPanel.add(secondButton);
        ActionListener secondListener = new ClickDecrement();
        secondButton.addActionListener(secondListener);

        firstFrame.add(firstPanel);
        firstFrame.setSize(200, 120);
        firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        firstFrame.setVisible(true);
    }
}

我想要访问的变量是“counter。”

3 个答案:

答案 0 :(得分:1)

从另一个类访问静态变量意味着使用类的名称继续变量名,因为static意味着它是一个类变量。因此,由于counter是JButtonTester类的静态变量,要从另一个类访问计数器,你会说JButtonTester.counter

JLabel counter = new JLabel("Count: " + JButtonTester.counter);

答案 1 :(得分:1)

你排在这里是错的

JLabel counter = new JLabel("Count: " + counter);

counter是对您正在创建的JLabel的引用,使用不同的变量名称

答案 2 :(得分:1)

您需要做的几件事:

  1. 重命名字段:您有两个名为counter的变量。
  2. 其次,您需要将JLabel移到actionPerformed方法之上并声明final,以便您可以在actionPerformed方法中访问它。
  3. 我没有看到您在面板中添加JLabel的位置,因此我添加了该行
  4. 这应该做你想要的:

    import java.awt.event.*;
    import javax.swing.*;
    
    public class JButtonTester {
    
        static int counter = 0;
    
        public static void main(String[] args) {
    
            final JLabel counter_label = new JLabel();
            class ClickCounter implements ActionListener {
    
                public void actionPerformed(ActionEvent event) {
                    counter++;
                    System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment");
                    counter_label.setText("Count: " + counter);
                }
            }
            class ClickDecrement implements ActionListener {
    
                public void actionPerformed(ActionEvent event) {
                    counter--;
                    System.out.println("Congratulations, you clicked a button " + counter + " time(s)! This might just be your greatest accomplishment");
                    counter_label.setText("Count: " + counter);
                }
            }
            JFrame firstFrame = new JFrame();
            JPanel firstPanel = new JPanel();
            firstPanel.add(counter_label);
    
            JButton firstButton = new JButton("Click me to increase your count!");
            firstPanel.add(firstButton);
            ActionListener firstListener = new ClickCounter();
            firstButton.addActionListener(firstListener);
    
            JButton secondButton = new JButton("Click me to decrease your count!");
            firstPanel.add(secondButton);
            ActionListener secondListener = new ClickDecrement();
            secondButton.addActionListener(secondListener);
    
            firstFrame.add(firstPanel);
            firstFrame.setSize(200, 120);
            firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            firstFrame.setVisible(true);
        }
    }