一种解决方案是使用SwingPropertyChangeSupport对象,使高度成为此支持对象的“绑定”属性,让您的GUI监听器对此模型类进行通知,从而将高度变化通知给GUI。
例如,
import java.beans.PropertyChangeListener;import javax.swing.event.SwingPropertyChangeSupport;public class Gravity implements Runnable { public static final String ALTITUDE = "altitude"; private SwingPropertyChangeSupport swingPcSupport = new SwingPropertyChangeSupport(this); private volatile double altitude; @Override public void run() { while (true) { double temp = altitude + 10; setAltitude(temp); // fires the listeners try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } public double getAltitude() { return altitude; } public void setAltitude(double altitude) { Double oldValue = this.altitude; Double newValue = altitude; this.altitude = newValue; // this will be fired on the EDT since it is a SwingPropertyChangeSupport object swingPcSupport.firePropertyChange(ALTITUDE, oldValue, newValue); } public void addPropertyChangeListener(PropertyChangeListener listener) { swingPcSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { swingPcSupport.removePropertyChangeListener(listener); }}有关更完整的可运行示例:
import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import javax.swing.*;import javax.swing.event.SwingPropertyChangeSupport;public class GravityTestGui extends JPanel { private static final long ALT_SLEEP_TIME = 400; private static final double ALT_DELTA = 5; JLabel altitudeLabel = new JLabel(" "); private Gravity gravity = new Gravity(ALT_SLEEP_TIME, ALT_DELTA); public GravityTestGui() { add(new JLabel("Altitude:")); add(altitudeLabel); gravity.addPropertyChangeListener(new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent pcEvt) { if (Gravity.ALTITUDE.equals(pcEvt.getPropertyName())) { String altText = String.valueOf(gravity.getAltitude()); altitudeLabel.setText(altText); } } }); new Thread(gravity).start(); } private static void createAndShowGui() { GravityTestGui mainPanel = new GravityTestGui(); Jframe frame = new Jframe("GravityTest"); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); }}class Gravity implements Runnable { public static final String ALTITUDE = "altitude"; private SwingPropertyChangeSupport swingPcSupport = new SwingPropertyChangeSupport(this); private volatile double altitude; private long sleepTime; private double delta; public Gravity(long sleepTime, double delta) { this.sleepTime = sleepTime; this.delta = delta; } @Override public void run() { while (true) { double temp = altitude + delta; setAltitude(temp); // fires the listeners try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } } public double getAltitude() { return altitude; } public void setAltitude(double altitude) { Double oldValue = this.altitude; Double newValue = altitude; this.altitude = newValue; // this will be fired on the EDT since it is a SwingPropertyChangeSupport object swingPcSupport.firePropertyChange(ALTITUDE, oldValue, newValue); } public void addPropertyChangeListener(PropertyChangeListener listener) { swingPcSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { swingPcSupport.removePropertyChangeListener(listener); }}


