本文介绍了更改jLabel可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Netbeans和Java的新手,并且jPanels上的jLabel存在问题.我有一个jPanel的jTabbedPane.我在jPanel上有一个jLabel.我想将jLabel的可见性设置为false,但是它似乎不起作用.当我运行程序时,该标签仍然可见.我不明白为什么.

I am new to Netbeans and Java and am having an issue with jLabels that are on jPanels. I have a jTabbedPane with a jPanel in it. I have a jLabel on the jPanel. I would like to set the visibility of the jLabel to false, but it does not seem to work. The label is still visible when I run the program. I do not understand why.

Label label = new Label("jLabel1");
label.setVisible(false);

推荐答案

您可以在initComponents();

package my.tt;

public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();

    }

    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1      = new javax.swing.JPanel();
        label        = new javax.swing.JLabel("jLabel1");
        label.setVisible(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        [...]

        jTabbedPane1.addTab("tab1", jPanel1);
        [...]

        pack();
    }

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            [...]
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JLabel label;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;

}

这篇关于更改jLabel可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:38