Java 如何在线程中为日历创建侦听器
我花了两天时间试图找到解决办法,但仍然不知道该怎么办 情况是这样的:我有一个runnable类,其中Calendar类型的变量通过无限循环随时间递增(源代码1中有一个简化模型)。接下来我有一个GUI,在这里我开始新的线程(源代码2) 当一个变量“时间”改变时,我想进行一些数学运算,并在GUI上更改一些标签 据我所知,我应该创建PropertyChangeListener,这是一个我有问题的地方:我真的不知道如何去做。我做了以下工作:将TimeLoop类更新为源代码3。我已经创建了源代码4中列出的侦听器(也简化了)。问题来了:我应该如何以及在哪里初始化侦听器?或者我错在哪里?谢谢你的回答 顺便说一句,我唯一的想法是(来源5),这当然行不通Java 如何在线程中为日历创建侦听器,java,listener,Java,Listener,我花了两天时间试图找到解决办法,但仍然不知道该怎么办 情况是这样的:我有一个runnable类,其中Calendar类型的变量通过无限循环随时间递增(源代码1中有一个简化模型)。接下来我有一个GUI,在这里我开始新的线程(源代码2) 当一个变量“时间”改变时,我想进行一些数学运算,并在GUI上更改一些标签 据我所知,我应该创建PropertyChangeListener,这是一个我有问题的地方:我真的不知道如何去做。我做了以下工作:将TimeLoop类更新为源代码3。我已经创建了源代码4中列出的
源1(我的可运行类的示例):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
源2(源于我的GUI类):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
源3(已编辑的可运行类):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
源4(侦听器):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
源代码5(错误代码):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
你快到了。您只需遵循正确的顺序:
#5中的代码存在的问题是,线程上运行的对象(new TimeLoop())与添加了PropertyChangeListener(tLoop)的TimeLoop实例不同。因此,线程中运行的实例不会触发任何事件。我认为使用将非常有用。我使用Observer Paten解决了这个问题。以下是代码: 源1(可运行类):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
源2(处理程序):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
源3(GUI):
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
}
public class TimeLoop implements Runnable {
private Calendar time = Calendar.getInstance();
private PropertyChangeSupport support = new PropertyChangeSupport(this);
public void updateTime() {
Calendar oldValue = time;
time.add(time.HOUR, 1);
Calendar newValue = time;
support.firePropertyChange("time", oldValue, newValue);
}
public void addListener(PropertyChangeListener listener) {
support.addPropertyChangeListener(listener);
}
@Override
public void run() {
try {
while (true) {
time.set(1997, 11, 27, 00, 00, 00);
updateTime();
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class TimeListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent pce) {
System.out.println(pce.getPropertyName() + " has new value);
}
}
public static void main(String args[]) {
Thread trTime = new Thread(new TimeLoop());
trTime.setName("TimeLoop");
trTime.start();
TimeLoop tLoop = new TimeLoop();
TimeListener tTistener = new TimeListener();
tLoop.addListener(tTistener);
}
public class TimeLoop extends Observable implements Runnable{
private Calendar time = Calendar.getInstance();
@Override
public void run() {
try {
time.set(1997, 11, 27, 00, 00, 00);
while (true) {
time.add(time.HOUR, 1);
setChanged();
notifyObservers(time);
Thread.sleep(1000);
}
}
} catch (InterruptedException ex) {
System.out.println("Interrupted error");
}
}
}
public class Handler implements Observer{
private Calendar resp;
@Override
public void update(Observable o, Object o1) {
if (o1 instanceof Calendar) {
resp = (Calendar) o1;
System.out.println("Received response: " + resp);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
final TimeLoop tLoop = new TimeLoop();
final Handler responseHandler = new Handler();
tLoop.addObserver(responseHandler);
Thread trTime = new Thread(tLoop);
trTime.setName("TimeLoop");
trTime.start();
}
此外,虽然这不是对您问题的直接回答,但将任务与任务的执行分离,即摆脱无限循环,并使用ScheduledThreadPoolExecutor来安排重复,代码可能会从中受益。Observer Paten非常有用。最后,我的代码开始工作了。非常感谢:)。