我正在尝试创建JFrame
和按钮所在的jLabel
以及我创建方法putTextNow
的另一个类,该方法将文本设置为{{1} }。我已经读过它应该使用多线程来完成,这在我看来更复杂。这是我的代码:
NewJFrame.java
jLabel
NewClass.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NewClass nc = new NewClass();
nc.putTextNow();
}
当我按下按钮时,它不起作用。它没有改变标签。我使用的是netbeans 8.0。这是我的完整代码
// NewJFrame.java
包test1;
公共类NewJFrame扩展了javax.swing.JFrame {
package test1;
public class NewClass {
public void putTextNow () {
NewJFrame nf = new NewJFrame();
nf.jLabel1.setText("OK!");
}
}
}
// NewClass.java
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
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(114, 114, 114)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jButton1)))
.addContainerGap(179, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(96, 96, 96)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(jButton1)
.addContainerGap(65, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NewClass nc = new NewClass();
nc.putTextNow();
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<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);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JLabel jLabel1;
// End of variables declaration
公共类NewClass {
public void putTextNow()
{
NewJFrame nf = new NewJFrame();
nf.jLabel1.setText(&#34; OK&#34);
}
}
答案 0 :(得分:4)
NewJFrame nf = new NewJFrame();
nf.jLabel1.setText("OK!");
在NewClass
班级中,您正在创建NewJFrame
的新实例。您正在尝试更新当前JFrame
(您正在看到的那个)的标签。您必须从JLabel
传递NewJFrame
的引用并从那里更新:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
NewClass nc = new NewClass();
nc.putTextNow(myJLabel);
}
和NewClass
变为
public class NewClass {
public void putTextNow (JLabel label) {
label.setText("OK!");
}
}
答案 1 :(得分:4)
如果我错了,请纠正我,但似乎您正在尝试创建包含JButton和JLabel的JFrame。单击按钮后,可以更改文本。
扩展Deutro所说的内容。基本上你想要遵循MVC模式(模型,查看器,控制器)。
这种模式的作用是将程序的各个部分分成易于定义的元素,这些元素允许分离关注点,这似乎是你所尝试的。
下面是如何设置MVC的模型。
首先是View类。
package mvctest;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.JButton;
public class View extends JFrame {
private JLabel lblNewLabel;
private JButton btnNewButton;
public View() {
lblNewLabel = new JLabel("New label");
getContentPane().add(lblNewLabel, BorderLayout.NORTH);
btnNewButton = new JButton("New button");
getContentPane().add(btnNewButton, BorderLayout.SOUTH);
}
public JButton getBtnNewButton() {
return btnNewButton;
}
public JLabel getLblNewLabel() {
return lblNewLabel;
}
}
Model类(MVC的逻辑)
package mvctest;
public class Model {
private View view;
public Model(View view) {
this.view = view;
}
public void changeText() {
view.getLblNewLabel().setText("Changed text");
}
}
最后是控制器
package mvctest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Controller {
public Controller() {
View view = new View();
Model model = new Model(view);
view.getBtnNewButton().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model.changeText();
}
});
}
}
答案 2 :(得分:1)
您不需要多线程。如果您的NewJFrame
课程有,您发布的代码应该有效
public
JLabel
,其名称为jLabel1
。
答案 3 :(得分:1)
正如已经指出的那样,你的代码应该已经有效了。此外,您不应该在NewJFrame类中公开您的JLabel。将其声明为私有,并使用getter和setter方法访问它。像这样:
public class NewJFrame extends JFrame{
private JLabel myLabel;
public String getMyLabel() {
return myLabel.getText();
}
public void setMyLabel(String string) {
myLabel.setText(string);
}
}
现在您可以使用以下标签设置标签:
nf.setMyLabel("Set my label to this string");