繁体   English   中英

JFrame组件之间的触发延迟

[英]Firing delay between JFrame components

我想展示如何使用JFrame在视觉上执行合并排序。 我要做的是延迟一些可见的后续JLabel 我尝试了很多方法,但所有这些都同时出现,没有中间延迟。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       // jLabel1.setVisible(false);
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jLabel5.setVisible(false);
        jLabel6.setVisible(false);
        jLabel7.setVisible(false);
        final Timer t=new Timer((4000), null);
         final int delay=2000;
        final ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {

          jLabel1.setVisible(true);
          t.getDelay();

          jLabel2.setVisible(true);
          t.setDelay(3000);

          jLabel3.setVisible(true);
          t.setDelay(2000);

          jLabel4.setVisible(true);
          t.setDelay(2000);

          jLabel5.setVisible(true);
          t.setDelay(2000);

          jLabel6.setVisible(true);
          t.setDelay(2000);
                }
  };

  new Timer(delay, taskPerformer).start();

但是,当我单击按钮时,尽管我一直保持延迟,所有标签都出现在相同的momenet中。

您需要更新计时器的动作侦听器的图标,如图所示这里 您可以实现的Icon界面来呈现其大小正比于元素的比较值图标,如图所示这里

附录: 能否请您具体一点?

您想动画化以初始随机顺序对大小为NList<Number>进行排序的中间步骤。 Number子类实现Comparable<T> ,因此compareTo()已经完成。 每个都有一个IconJLabel GridLayout(1, 0)可用于显示值。 DecRenderer显示了如何按比例创建图标。 您需要在[0, N)的间隔内改变高度。 GrayIcons &Mad的示例演示如何以某种顺序对图标的显示进行动画处理。

有多种原因导致此方法无法正常工作。 首先, javax.swing.Timer无法以这种方式工作。 它在后台等待,直到给定的延迟过去,然后调用已注册的ActionListeneractionPerformed方法。

其次,如果它确实以这种方式工作,它将阻止事件分派线程,从而阻止其处理重画请求。

我想您会发现如何使用Swing计时器

public class BlinkOut {

    public static void main(String[] args) {
        new BlinkOut();
    }

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

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class TestPane extends JPanel {

        private JLabel[] labels;
        private int[] delays;
        private Timer timer;
        private int index;

        public TestPane() {
            setLayout(new GridLayout(0, 1));
            labels = new JLabel[7];
            for (int index = 0; index < 7; index++) {
                labels[index] = new JLabel("Label " + (index + 1));
                add(labels[index]);
            }
            delays = new int[] {2000, 3000, 2000, 2000, 2000, 2000, 2000};
            JButton hide = new JButton("Hide");
            add(hide);
            hide.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Click");
                    index = 0;
                    labels[index].setVisible(false);
                    timer.setDelay(delays[index]);
                    timer.start();
                }
            });
            timer = new Timer(delays[0], new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Tick");
                    timer.stop();
                    index++;
                    if (index < 7) {
                        labels[index].setVisible(false);
                        timer.setDelay(delays[index]);
                        timer.start();
                    }
                }
            });
            timer.setRepeats(false);
            timer.setCoalesce(true);
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM