Java Swing - 从另一种方法设置 Jlabel 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9761449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:21:05  来源:igfitidea点击:

Java Swing - set Jlabel text from another method

javaswingjlabelsettext

提问by Bagshot

I'm pretty new to Java and Swing, and I'm using Windowbuilder to play around with a few GUI ideas I have, but I've run into a problem when trying to set the text of a Jlabel.

我对 Java 和 Swing 还很陌生,我正在使用 Windowbuilder 来尝试一些我拥有的 GUI 想法,但是在尝试设置 Jlabel 的文本时遇到了问题。

Windowbuilder has automatically created an instance of the Jlabel, called pathLabel, in the initialize() method like so:

Windowbuilder 在 initialize() 方法中自动创建了 Jlabel 的一个实例,称为 pathLabel,如下所示:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 570, 393);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel pathLabel = new JLabel("New label");
    pathLabel.setBounds(61, 296, 414, 15);
    frame.getContentPane().add(pathLabel);}

If I use pathLabel.setText("enter text here") from within this initialize() method, then it works fine, but how can I set text from a completely different method? It's not letting me reference it.

如果我在这个 initialize() 方法中使用 pathLabel.setText("enter text here"),那么它工作正常,但我如何从一个完全不同的方法设置文本?它不让我参考它。

I never had this problem in Visual Studio with C#, and was able to set the text of a label from any method I choose. What am I missing?

我在使用 C# 的 Visual Studio 中从未遇到过这个问题,并且能够通过我选择的任何方法设置标签的文本。我错过了什么?

I hope this makes sense, and I appreciate any help at all. Thanks.

我希望这是有道理的,我非常感谢任何帮助。谢谢。

采纳答案by mohit jain

You can put pathLabel as a instance variable in your class and access it across all the methods in the class.

您可以将 pathLabel 作为类中的实例变量,并在类中的所有方法中访问它。

class GUIClass extends JFrame{
 JLabel pathLabel;
 private void initialize() {
   frame = new JFrame();
   frame.setBounds(100, 100, 570, 393);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.getContentPane().setLayout(null);

   pathLabel = new JLabel("New label");
   pathLabel.setBounds(61, 296, 414, 15);
   frame.getContentPane().add(pathLabel);
}
void func(){
   pathLabel.setText("enter text here");
}

回答by Gabriel Gonzalez

You can create a field for pathLabel in the surrounding class so that all class methods can access it:

您可以在周围的类中为 pathLabel 创建一个字段,以便所有类方法都可以访问它:

class YourClass {
    private JLabel pathLabel;
    private void initialize() {
        ...
        // Note that there is no declaration for pathLabel inside initialize
        //   since it was already declared above, and the above
        //   declaration is a reference shared by all class methods
        pathLabel = new JLabel("New label");
        ...}   
}