栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过不同的线程访问变量和摆动组件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过不同的线程访问变量和摆动组件

一种解决方案是使用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);   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496372.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号