Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从运行方法/线程返回数据以更新GUI_Java_Multithreading - Fatal编程技术网

Java 从运行方法/线程返回数据以更新GUI

Java 从运行方法/线程返回数据以更新GUI,java,multithreading,Java,Multithreading,我有一个主要的方法,就是根据乐高机器人的数据绘制地图, 主线程应该处理从机器人获取的数据: public static void main(String[] args) { final Map map = new SimpleMap(); try { // Set System Look and Feel UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

我有一个主要的方法,就是根据乐高机器人的数据绘制地图, 主线程应该处理从机器人获取的数据:

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();
}
}


如何从线程获取数据以更新GUI?

您不能从线程的run方法返回值。 您所能做的就是在run中调用另一个方法,并将该值作为该方法中的参数传递,然后执行您想要执行的任何处理。 我希望它能帮助你。
如果未解决,请随时询问。

您可以将框架传递给线程。
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);
}