【问题标题】:Make static variable accessible to JLabel?让 JLabel 可以访问静态变量?
【发布时间】:2014-01-26 05:27:25
【问题描述】:

我在玩 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”。

【问题讨论】:

  • 来自 BlueJ 的错误消息:“变量计数器可能尚未初始化”在这一行:JLabel counter = new JLabel("Count: " + counter);

标签: java jframe jpanel jlabel


【解决方案1】:

你需要做的几件事:

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

这应该做你想做的:

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);
    }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 哇,感谢乔希的快速和超级有用的回复。有效!不过,我不太确定为什么这是必要的。变量不是在main方法的范围内吗?
    • 此解决方案有效,因为您有两个名为 counter 的变量。一个是int,另一个是JLabel。如果您将标签重命名为 counter_label,它也会起作用。
    • 感谢@ryvantage 的澄清
    • 我会,只要我被允许(3 分钟)@ryvantage,非常感谢。我还有一个问题:如何让 ClickCounter 和 ClickDecrement 中的 actionListener 方法更新 JLabel?
    • 你需要使用JLabel中的setText()方法。
    【解决方案3】:

    你说错了

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 2018-11-02
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 2012-06-29
      • 2021-11-24
      相关资源
      最近更新 更多