gpt4 book ai didi

java - 使用 beans 绑定(bind)更新 JLabel 时出现问题

转载 作者:行者123 更新时间:2023-12-01 15:54:15 24 4
gpt4 key购买 nike

我正在尝试使用 netbeans IDE 来使用 beans 绑定(bind)。我想更新标签中的文本。这是我创建的 bean。

public class SystemTimeBean implements Serializable {

public static final String PROP_SAMPLE_PROPERTY = "systemTime";

private String systemTime;

private PropertyChangeSupport propertySupport;

public SystemTimeBean() {
propertySupport = new PropertyChangeSupport(this);
}

public String getSystemTime() {
return systemTime;
}

public void setSystemTime(String value) {
String oldValue = systemTime;
systemTime = value;
propertySupport.firePropertyChange(PROP_SAMPLE_PROPERTY, oldValue, systemTime);
}


public void addPropertyChangeListener(PropertyChangeListener listener) {
propertySupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
propertySupport.removePropertyChangeListener(listener);
}

}

public class SystemTimeModel {

private long systemTime;
private SystemTimeBean bean;

public SystemTimeModel() {
bean = new SystemTimeBean();
}

public long getSystemTime() {
return systemTime;
}

public void setSystemTime(long systemTime) {
this.systemTime = systemTime;
bean.setSystemTime(Long.toString(systemTime));
}

}

在我的 JFrame 中绑定(bind)的代码

bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

systemTimeBean1 = new beansbindingapp.SystemTimeBean();
lblBinding = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, systemTimeBean1, org.jdesktop.beansbinding.ELProperty.create("${systemTime}"), lblBinding, org.jdesktop.beansbinding.BeanProperty.create("text"));
bindingGroup.addBinding(binding);

bindingGroup.bind();

和Main.class

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
JFrame frame = new SystemTimeFrame();
frame.setVisible(true);
}
});

SystemTimeModel time = new SystemTimeModel();

for(int i = 0; i < 10; i++) {
time.setSystemTime(System.currentTimeMillis());
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

}

标签未更新。感谢您的帮助。

最佳答案

我最近一直在使用 bean 绑定(bind),刚刚解决了代码中的读/写问题。我稍微修改了您的 Main 代码,通过更改 SystemTimeBean 来更新时间。我相信您的代码的问题是您没有更新绑定(bind)的对象。

public class Main {

private JFrame _frame;
private SystemTimeModel _systemTimeModel = new SystemTimeModel();

public static void main( final String[] args ) {
final Main systemTimeFrame = new Main();

systemTimeFrame._frame.setVisible( true );

systemTimeFrame.doStuff();
}

public Main() {
initialize();
}

public void doStuff() {
for( int i = 0; i < 10; i++ ) {
_systemTimeModel.setSystemTime( System.currentTimeMillis() );
try {
Thread.sleep( 1000 );
}
catch ( InterruptedException ex ) {
Logger.getLogger( Main.class.getName() ).log( Level.SEVERE, null, ex );
}
}
}

private void initialize() {
_frame = new JFrame();
_frame.setBounds( 100, 100, 450, 300 );
_frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

JLabel lblBinding = new JLabel( "" );
_frame.add( lblBinding );

BindingGroup bindingGroup = new BindingGroup();
BeanProperty< SystemTimeBean, String > sourceProperty = BeanProperty.create( "systemTime" );
BeanProperty< JLabel, String > targetProperty = BeanProperty.create( "text" );
AutoBinding< SystemTimeBean, String, JLabel, String > binding =
Bindings.createAutoBinding( UpdateStrategy.READ_WRITE, _systemTimeModel.getBean(), sourceProperty, lblBinding, targetProperty );
binding.bind();
bindingGroup.addBinding( binding );
}

}

关于java - 使用 beans 绑定(bind)更新 JLabel 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5401824/

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