【发布时间】:2014-07-15 09:24:41
【问题描述】:
每当调用线程中的 run 方法时,我的 GUI 就会冻结,有人知道为什么吗?
主要:
try {
// Set System Look and Feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException e) {
// handle exception
} catch (ClassNotFoundException e) {
// handle exception
} catch (InstantiationException e) {
// handle exception
} catch (IllegalAccessException e) {
// handle exception
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame(null, null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
从线程运行方法:
public void run() {
while (true) {
System.out.println("test");
}
}
应该启动线程的actionListener:
private ActionListener btnStartListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
robot.run();
}
};
public class RobotThread implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("test");
}
}
}
【问题讨论】:
标签: java multithreading user-interface