Java Netbeans Jlabel 隐藏和显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23548603/
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-13 23:48:56  来源:igfitidea点击:

Java Netbeans Jlabel Hide and Show

javajlabelnetbeans-7

提问by MyCloud

I have a Jframe form and once this form runs i would like some specfeic labels become invisble until the user press on the "Submit button" they become visible.

我有一个 Jframe 表单,一旦这个表单运行,我希望一些特定的标签变得不可见,直到用户按下“提交按钮”它们变得可见。

Assuming that i have 2 JLabels named Label1 and Label2

假设我有 2 个 JLabel,分别命名为 Label1 和 Label2

The idea in my mind would be to go for the constructor of the JFrame class and write

我的想法是寻找 JFrame 类的构造函数并编写

Label1.setVisible(false);
Label2.setVisible(false);

Then at the ActionPerformedfunction of the Submit button

然后在ActionPerformed提交按钮的功能

Label1.setVisible(true);
Label2.setVisible(true);

But this option does not work and netbeans keeps showing several errors, the question is does my idea is not the proper way to do it or i'm just doing it wrong?

但是这个选项不起作用,netbeans 不断显示几个错误,问题是我的想法不是正确的方法还是我只是做错了?

采纳答案by Ahmad Vatani

make sure that you put

确保你把

Label1.setVisible(false);
Label2.setVisible(false); 

after the initComponents();method call! for example if your JFrame name is: NewJFrame you should change constructor with this code:

initComponents();方法调用之后!例如,如果您的 JFrame 名称是:NewJFrame,您应该使用以下代码更改构造函数:

public NewJFrame() {

        initComponents();

        Label1.setVisible(false);
        Label2.setVisible(false);
    }

回答by Stenart

Have you tried typing Label1.setVisible(true);for example? (Notice the ';' mark)

Label1.setVisible(true);例如,您是否尝试过打字?(注意';'标记)

Or check what errors comes up and post it here.

或者检查出现了什么错误并将其张贴在这里。