运行 按下按钮时的线程

Run thread on button press

我有一个带有以下按钮的 GUI。当它被按下时,它应该 运行 一个新线程(我有一个 class 实现了 Runnable)。但是,当我这样做时,GUI 会冻结。我做错了什么,我该如何解决?

        //button
        addKitchenStaff.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                //create object of a class which implements runnable
                KitchenStaff kitchenStaff = new KitchenStaff(allDishes, ingredientsModel, dishesModel,communication.getBap());
                //arraylist of all such objects
                allKitchenStaff.add(kitchenStaff);

                Thread thread = new Thread(kitchenStaff);
                thread.run();
            }
        });

要运行一个线程,你应该调用start()方法。

尝试 thread.start() 而不是 thread.run()

run() 在调用 Thread 上工作。使用 start() 代替