更改jLabel可见性

时间:2014-04-14 14:58:47

标签: java netbeans jpanel jlabel jtabbedpane

我是Netbeans和Java的新手,我遇到了jPanel上的jLabel问题。我有一个带有jPanel的jTabbedPane。我在jPanel上有一个jLabel。我想将jLabel的可见性设置为false,但它似乎不起作用。运行程序时,标签仍然可见。我不懂为什么。

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

1 个答案:

答案 0 :(得分:0)

您可以在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;

}