繁体   English   中英

我们可以从 Java 中的另一个类更改 JLabel 吗?

[英]Can we change a JLabel from another class in java?

我是 Java 新手,我正在尝试使用JLabelJButton 我的问题是我无法更改actionPerformed()方法中的JLabel 它找不到变量nbr 请帮忙 :(

package com.test;

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener{

    int x = 0;

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());
        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        add(button1);
        JLabel nbr = new JLabel(x);
        add(nbr);
    }

    public void actionPerformed(ActionEvent e){
        x++;
        nbr.setText(x);
    }
}

那是因为您正在访问一个处于不同且有限范围内的变量。 在您的情况下,您的nbr是在Main()函数中定义的,并且仅限于该范围。 如果你想访问它,你必须使它成为全局的,这意味着你必须在你的类中声明它。

例子:

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener{

    int x = 0;
    private JLabel nbr; // declare a global variable

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        nbr = new JLabel(); // initialize here...

        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());
        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        add(button1);
        nbr.setText(x); // set text to that variable here...
        add(nbr);
    }

    public void actionPerformed(ActionEvent e){
        x++;
        nbr.setText(x);
    }
}

Ps:检查是否有任何语法错误,我没有测试就写了这个。 :)

如果您不想让nbr成为类Main的成员,而是将其作为类构造函数中的局部变量保留,您仍然可以使用Swing API 从actionPerformed()方法更新JLabel文本。 下面的示例代码。 请注意, JLabel构造函数及其setText()方法都需要String参数而不是int参数,因此您发布的代码无法编译。 我建议您阅读如何创建最小的、可重现的示例 无论如何,我已经在下面的代码中修复了该错误。

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame implements ActionListener {
    int x = 0;

    public static void main(String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());
        JButton button1 = new JButton("Button1");
        button1.addActionListener(this);
        add(button1);
        JLabel nbr = new JLabel(String.valueOf(x));
        add(nbr);
    }

    public void actionPerformed(ActionEvent e){
        x++;
        Container contentPane = getContentPane();
        Component[] cmpts = contentPane.getComponents();
        for (Component cmpt : cmpts) {
            if (cmpt instanceof JLabel) {
                JLabel nbr = (JLabel) cmpt;
                nbr.setText(String.valueOf(x));
            }
        }
    }
}

你也可以尝试这样的事情:

public class Main extends JFrame {

    public static void main(final String[] args) {
        new Main().setVisible(true);
    }

    public Main(){
        super("Test");
        setSize(640, 480);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(new FlowLayout());

        final JButton button1 = new JButton("Button1");
        final JLabel  nbr     = new JLabel("0");
        add(button1);
        add(nbr);

        final AtomicInteger x = new AtomicInteger();

        button1.addActionListener(e -> {
            nbr.setText("" + x.incrementAndGet());
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM