0

我有一个在 EDT 中执行的方法。是这样的:

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

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

我期望发生的事情:十个 JPanel 将从它们的父级中一个一个地删除,每次删除之间会有三秒钟的停顿......

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

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

既然我在 EDT 中做这一切,为什么没有即时更改?为什么它要先等待方法结束?

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

4

2 回答 2

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);
        }
    }
}
于 2013-03-01T02:10:56.760 回答
4

这是因为 UI 线程负责绘制屏幕,​​以及处理事件。当您让 UI 线程进入睡眠状态时,就不会再发生绘画或事件处理了。当线程恢复绘画时,所有面板都已被移除。

这个 URL 非常全面地介绍了如何使用 Swing 处理线程

您最好创建一个删除面板的线程。

(未经测试)

Thread t = new Thread() {
   @Override
   public void run() {  // override the run() for the running behaviors
     for (int i = 0; i < 10; i++)
     {
        Thread.sleep(3000);
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
           someJPanel.remove(...);
          });
     }
   }
};
t.start();  // call back run()
于 2013-03-01T02:13:46.043 回答