EDT摆动问题

Kar*_*120 0 java swing event-dispatch-thread

我有一个在EDT内执行的方法.它是这样的:

myMethod()
{
  System.out.prinln(SwingUtilities.isEventDispatchThread());

  for (int i = 0; i < 10; i++)
  {
   Thread.sleep(3000);
   someJPanel.remove(otherJPanel);
  }
}
Run Code Online (Sandbox Code Playgroud)

我期望发生的事情:十个JPanels将逐一从他们的父母中删除,每次删除之间有三秒钟暂停...

实际发生了什么:在所有10个元素一次被移除之后,一切都冻结了30秒.

控制台中的行始终为true(SwingUtilities.isEventDispatchThread()).

由于我在EDT中做了这一切,为什么瞬间变化呢?为什么它等待方法首先达到它的结束?

我应该如何改变我的代码以实现删除之间的三秒延迟?

Mad*_*mer 7

Swing使用单个线程来分派事件并处理重绘请求.每次阻止此线程时,都会阻止EDT处理这些重新绘制请求,使UI看起来像"已停止".

相反,使用类似a的东西javax.swing.Timer来插入延迟并执行操作.这将在EDT中执行操作,但将在后台线程中等待.

有关详细信息,请阅读事件调度线程 ...

更新了计时器示例

public class SlowDecline {

    public static void main(String[] args) {
        new SlowDecline();
    }
    private TestPane last;

    public SlowDecline() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                TestPane parent = new TestPane(Color.RED);
                TestPane tp = add(parent, Color.BLUE);
                tp = add(tp, Color.GREEN);
                tp = add(tp, Color.CYAN);
                tp = add(tp, Color.LIGHT_GRAY);
                tp = add(tp, Color.MAGENTA);
                tp = add(tp, Color.ORANGE);
                tp = add(tp, Color.PINK);

                last = tp;

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(parent);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (last != null) {
                            Container parent = last.getParent();
                            if (parent != null) {
                                parent.remove(last);
                                parent.repaint();
                                if (parent instanceof TestPane) {
                                    last = (TestPane) parent;
                                }
                            } else {
                                last = null;
                            }
                        } else {
                            (((Timer)e.getSource())).stop();
                        }
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
        });
    }

    public TestPane add(TestPane parent, Color color) {

        TestPane child = new TestPane(color);
        parent.add(child);
        return child;
    }

    public class TestPane extends JPanel {

        public TestPane(Color background) {
            setLayout(new BorderLayout());
            setBorder(new EmptyBorder(10, 10, 10, 10));
            setBackground(background);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(50, 50);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)