我创建了JFrame
哪些包含并InternalFrame
绘制了正在移动的图形(每个图形都是另一个Thread
)我想让pauseButton
它暂停,所以我想在传递的对象上同步它们。
但是当我点击暂停按钮时,整个窗口都冻结了,我无法点击播放按钮另一件事是当时只有一个正在运行,我希望它们都运行然后全部暂停。
class A extends JFrame{
....
Object o = new Object();
JButtton pauseButton = new JButton("pause");
JButtton playButton = new JButton("play");
B b = new B(o);
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (synchronizator) {
try {
synchronizator.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
synchronized (synchronizator) {
synchronizator.notifyAll();
}
}
...
}
class B extends JInternalFrame{
Object o;
B(Object o){this.o = o}
./...
many... C thread = new C(o);
....
}
class C extends Thread{
Object o;
booolean running;
public void run(){
while(running){
synchronized(o){
}
}
}
}