我有两个班级List[List[_]]
和MainFrame
。 Test
类包含两个组件MainFrame
和JButton
。
我想通过在JLabel
类中创建JLabel
的实例,从setText()
类调用Test
方法时更改MainFrame
的文本,当我点击Test
时。但遗憾的是,JButton
和setText()
都没有来自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;
}
答案 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);
}