从另一个 class 更改 jLabel-text

Change jLable-text from another class

我的问题:我无法从另一个 class 更改 jLabel 的文本。在我的名为 "NewJFrame" 的 JFrame-Class 中,我定义了一个名为 "setLabelText()" 的方法,其中更改了 jLable1 的文本。这个方法是从 main-Method 调用的,但是文本没有改变。怎么了?非常感谢您的帮助!

public class Main {

    static NewJFrame f = new NewJFrame();

    public static void main(String[] args) {

        f.main(null);
        f.setLabelText("changedText");

    }

}

这里是我的新 JFrame class,里面有很多生成的东西。重要的是 setText()-方法。

 public class NewJFrame extends javax.swing.JFrame {


    public NewJFrame() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(156, 156, 156)
                .addComponent(jLabel1)
                .addContainerGap(203, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(152, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(132, 132, 132))
        );

        pack();
    }// </editor-fold>                        



    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        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.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }


    public void setLabelText (String text){
        jLabel1.setText(text);

    }



    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}

当您创建 JFrame 框架(来自 netbeans)时,它已经具有其主要功能。因此,要么您必须创建 JPanel 并在具有主要功能的 class 中手动初始化,或者您可以移动 f.setText("XYZ");到 NewJFrame() class。 就像:

public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        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.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ***NewJFrame f=new NewJFrame();
f.setVisible(true);
f.setText("Your Text Goes Here");***
            }
        });
    }

I need to call the setText()-method from Main-class

f.main(null); 的用途是什么,您正试图在这一行中将文本设置为 JFramef.setText("changedText");

作为解决方案,将您的 JLabel 访问修饰符更改为 public。默认情况下它是 private,因此您不能在另一个 class.

中更改它

变化:

private javax.swing.JLabel jLabel1;

收件人:

public javax.swing.JLabel jLabel1;

在 netbenas 中,默认情况下您无法编辑生成的代码。因此,要更改访问修饰符,右键单击设计 window* 中的 JLabel,然后转到 自定义代码 。在 window 中将 Access: 更改为 public.

现在,将 Main class 更改如下:

public class Main {
    static NewJFrame f = new NewJFrame();

    public static void main(String[] args) {
        f.jLabel1.setText("changedText");//this line will get the label in NewJFrame and set the text
        //f.setVisible(true);//this is added to open the frame. May be you dont want this line
    }
}