从另一个类更改JLabel文本不起作用

时间:2015-09-26 09:59:37

标签: java swing jlabel

我有两个班级List[List[_]]MainFrameTest类包含两个组件MainFrameJButton

我想通过在JLabel类中创建JLabel的实例,从setText()类调用Test方法时更改MainFrame的文本,当我点击Test时。但遗憾的是,JButtonsetText()都没有来自getText()类。我怎样才能实现它?

这是我的代码。

MainFrame.java

Test

Test.java

    public class MainFrame extends javax.swing.JFrame {

    public MainFrame() {
        initComponents();
    }

    private void initComponents() {

        labelToSet = new javax.swing.JLabel();
        clickMeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        labelToSet.setText("Hello");

        clickMeButton.setText("Click me");
        clickMeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clickMeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(84, Short.MAX_VALUE)
                .addComponent(clickMeButton)
                .addGap(50, 50, 50)
                .addComponent(labelToSet)
                .addGap(110, 110, 110))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelToSet)
                    .addComponent(clickMeButton))
                .addContainerGap(79, Short.MAX_VALUE))
        );

        pack();
    }                       

    /*Code to be concentrated*/
    private void clickMeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Test t = new Test();
    }                                             

    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.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

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

    public javax.swing.JButton clickMeButton;
    public javax.swing.JLabel labelToSet;                   
}

1 个答案:

答案 0 :(得分:4)

您正在更改类labelToSet不同实例中的MainFrame文本,这是一个从未在屏幕上显示的实例。

如果您希望Test的构造函数中的代码更改已显示的标签文本,则需要向其传递对该MainFrame对象的引用。

更改您的Test构造函数以接受该引用:

public class Test {
    Test(MainFrame mf) {
        mf.labelToSet.setText("Goodbye!");
    }
}

并相应地调用它(将引用this传递给当前显示的MainFrame对象):

private void clickMeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    Test t = new Test(this);
}