gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:11:20 26 4
gpt4 key购买 nike

我有两个类MainFrameTestMainFrame类包含两个组件 a JButton和一个 JLabel .

我想要JLabel的文本当我调用 setText() 时进行更改方法来自Test类,通过创建 MainFrame 的实例在Test类,当我单击JButton时。但不幸的是 setText() 都没有也不是 getText()Test 开始工作类(class)。我怎样才能实现它?

这是我的代码。

MainFrame.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;
}

测试.java:

public class Test {

Test() {
MainFrame mf = new MainFrame();
System.out.println(mf.labelToSet.getText());
mf.labelToSet.setText("Goodbye!");
}
}

最佳答案

您正在 MainFrame 类的不同实例中更改 labelToSet 的文本,该实例永远不会显示在屏幕上。

如果您希望 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);
}

关于java - 从另一个类更改 JLabel 文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32795900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com