我正在尝试使用在 TimerTask 中更新的 Bean 类提供的值更新 JLabel 中显示的文本。
Bean 得到了很好的更新,但 JLabel 没有。
为了说明这个问题,我构建了一个由以下组成的小项目:
- 设置和获取将与我的 JLabel 绑定的字符串变量的类 Bean
package test;
public class Bean {
private String value;
public Bean() {
setValue("Created");
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
- TimerTask (UpdateTask) 用于更新 Bean 中的变量(在其值中添加一个 'X')
package test;
import java.util.TimerTask;
public class UpdateTask extends TimerTask{
private Bean bean;
public UpdateTask(Bean bean) {
this.bean=bean;
}
@Override
public void run() {
System.out.println("UpdateTask - Bean = " + bean.getValue());
bean.setValue(bean.getValue() +"X");
System.out.println("UpdateTask - Bean = " + bean.getValue());
}
}
- 一个 TimerTask (ViewTask) 只用于输出控制台中 Bean 变量和 JLabel 的值
package test;
import java.util.TimerTask;
import javax.swing.JLabel;
public class ViewTask extends TimerTask{
private JLabel lbl;
private Bean bean;
public ViewTask(JLabel lbl, Bean bean) {
this.lbl=lbl;
this.bean=bean;
}
@Override
public void run() {
System.out.println("ViewTask - Bean:" + bean.getValue());
System.out.println("ViewTask - Label:" + lbl.getText());
}
}
- 显示带有 JLabel 的 Frame 的主类,创建 Bean,初始化 DataBinding 并启动我的 2 个计时器
package test;
import java.awt.EventQueue;
import java.util.Timer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
public class Panel {
private JFrame frame;
private Bean bean;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Panel window = new Panel();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Panel() {
initialize();
bean =new Bean();
new Timer().schedule(new ViewTask(lblNewLabel, bean), 0, 1000);
new Timer().schedule(new UpdateTask(bean), 0, 1000);
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 100, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
lblNewLabel = new JLabel("New label");
frame.getContentPane().add(lblNewLabel);
initDataBindings();
}
protected void initDataBindings() {
BeanProperty<Bean, String> beanBeanProperty = BeanProperty.create("value");
BeanProperty<JLabel, String> jLabelBeanProperty = BeanProperty.create("text");
AutoBinding<Bean, String, JLabel, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, bean, beanBeanProperty, lblNewLabel, jLabelBeanProperty);
autoBinding.bind();
}
}
- windowsBuilder中显示的我的绑定属性:
在运行时,控制台中的输出如下:
ViewTask - Bean:Created
UpdateTask - Bean = Created
ViewTask - Label:New label
UpdateTask - Bean = CreatedX
UpdateTask - Bean = CreatedX
UpdateTask - Bean = CreatedXX
ViewTask - Bean:CreatedX
ViewTask - Label:New label
UpdateTask - Bean = CreatedXX
UpdateTask - Bean = CreatedXXX
ViewTask - Bean:CreatedXX
ViewTask - Label:New label
UpdateTask - Bean = CreatedXXX
UpdateTask - Bean = CreatedXXXX
ViewTask - Bean:CreatedXXX <--- Bean is updated
ViewTask - Label:New label <--- Label is not updated
有人可以帮我找出问题所在吗?谢谢