我有一個在swing中使用線程的問題。 我會先解釋問題,然後我會發布我的代碼。Java Swing Threading問題
問題是以下幾點。我有兩個類,一個包含業務邏輯,另一個包含用戶界面。基於接口中的用戶操作,我調用包含業務邏輯的類中的方法。 但是,我的下一個任務是讓用戶點擊一個UI元素,並且基於該點擊,我將一個新的鼠標監聽器附加到其他UI元素,然後等待用戶單擊該其他UI元素,之後,我想要更改初始點擊的UI元素。 但是,我的應用程序在單擊其他UI元素後會阻止。 在示例代碼中,初始UI元素和其他UI元素都是相同的,但通常它們不會。
示例類:
======================================= =============================
package threadtests;
import java.awt.Color;
public class LogicClass extends Thread {
FrameClass fc;
SelectSynchronizerObject mysso;
public void run() {
while(true);
}
public void startThisFromFrame() {
MyMouseListener2 m2 = new MyMouseListener2();
m2.initialize(fc.jp, mysso);
fc.jp.addMouseListener(m2);
mysso.getSelected();
fc.jp.setBackground(new Color(100,100,100));
fc.jp.removeMouseListener(m2);
}
public static void main(String args[]) {
SelectSynchronizerObject sso = new SelectSynchronizerObject();
LogicClass lc = new LogicClass();
lc.mysso = sso;
lc.start();
FrameClass fc = new FrameClass();
fc.lc = lc;
lc.fc = fc;
fc.mysso = sso;
fc.initialize(lc);
}
}
============== ================================================== ====
package threadtests;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrameClass extends JFrame {
LogicClass lc;
SelectSynchronizerObject mysso;
JPanel jp = new JPanel();
public void initialize(LogicClass arg) {
lc = arg;
setSize(100,100);
MyMouseListener m = new MyMouseListener();
jp.addMouseListener(m);
m.initialize(jp, lc);
jp.setBackground(new Color(255,200,200));
add(jp);
setVisible(true);
}
}
======================================= =============================
package threadtests;
public class SelectSynchronizerObject {
public int selectednumer = 0;
public boolean numberset = false;
public synchronized void panelSelected(int a) {
selectednumer = a;
notify();
}
public synchronized int getSelected() {
try {
wait();
} catch (InterruptedException e) {
System.out.println(e);
}
return selectednumer;
}
}
============================================== ======================
package threadtests;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class MyMouseListener implements MouseListener {
JPanel mypanel;
LogicClass lc;
public void initialize(JPanel arg, LogicClass arg2) {
mypanel = arg;
lc = arg2;
}
public void mouseClicked(MouseEvent e) {
lc.startThisFromFrame();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
mypanel.setBackground(new Color(200,255,200));
}
public void mouseExited(MouseEvent e) {
mypanel.setBackground(new Color(255,200,200));
}
}
===================== ===============================================
package threadtests;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
public class MyMouseListener2 implements MouseListener {
JPanel mypanel;
SelectSynchronizerObject sso;
public void initialize(JPanel arg, SelectSynchronizerObject arg1) {
mypanel = arg;
sso = arg1;
}
public void mouseClicked(MouseEvent e) {
sso.panelSelected(1);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
==========================
感謝您的幫助。
看看SwingUtilities.invokeLater – MeBigFatGuy 2011-04-28 19:47:28
其實,我相信OP需要[SwingWorker](http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html),這是爲了在後臺執行昂貴的任務。'invokeLater'意味着排隊由其他線程完成的任務,這些線程需要更新UI(只能通過EDT進行) – 2011-04-28 19:54:40
Nevermind,我重新閱讀了這個問題,它看起來像OP想要創建新的UI無論如何最好通過EDT完成的元素 - 'invokeLater'看起來就像在這種情況下要走的路。 – 2011-04-28 19:56:48