Change jLabel Visibility

2019-08-25 19:59发布

问题:

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);

回答1:

You can set it inside 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;

}