public static void main(String[] args) {
final Map map = new SimpleMap();
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(map);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
MainThread Main = new MainThread();
Thread main = new Thread(Main);
main.start();
}
我有这个主要方法,它应该从乐高机器人的数据中绘制 map , 主线程应该处理从机器人获取的数据:
public class MainThread implements Runnable {
@Override
public void run() {
final Map map = new SimpleMap();
Point pos = new Point(0, 0);
int[] measured = { 0x40, 0x00, 0x02, 0x13, 0x00, 0x03, 0x23, 0xFF,
0x30, 0x2E, 0x2D, 0x2E, 0x30, 0x3F, 0x3E, 0x3C, 0x3C, 0x3C,
0x3D, 0x3D, 0x3F, 0x5E, 0x5F, 0x5F, 0xFE, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x6B, 0x69, 0x68, 0x68, 0x69, 0x69, 0x6C, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
map.put(new Square(pos, false));
//
// System.out.println("measure");
// int[] measured;
// try {
// measured = robot.measure();
// } catch (CommandErrorException e) {
// e.printStackTrace();
// return;
// }
map.processMeasureData(measured, pos, Orientation.NORTH);
}
}
如何从线程获取数据来更新 GUI?
最佳答案
您无法从线程的 run 方法返回值。 您所能做的就是在运行中调用另一个方法,并将该值作为该方法中的参数传递,然后执行您想要执行的处理。 我希望它能帮助你。 如果没有解决请随时再次询问。
关于java - 从运行方法/线程返回数据以更新 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24384903/