1

我想展示合并排序如何在视觉上使用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();

但是,当我单击按钮时,所有标签都出现在同一个时刻,尽管我一直在延迟。

4

2 回答 2

3

您需要更新计时器的动作侦听器中的图标,如此处所示。您可以实现该接口以呈现大小与元素的比较值成比例的图标,如此Icon所示。

附录:你能说得具体一点吗?

您希望以最初的随机顺序对 aList<Number>大小排序的中间步骤进行动画处理。子类 implement ,所以已经完成了。每个具有 的A可用于显示值。展示了如何创建具有比例大小的图标;您需要在区间内改变高度。& Mad 的示例展示了如何以某种顺序对图标的显示进行动画处理。NNumberComparable<T>compareTo()GridLayout(1, 0)JLabelIconDecRenderer[0, N)GrayIcons

于 2013-01-21T04:39:02.820 回答
2

There are a number of reasons why this won't work. Firstly, javax.swing.Timer doesn't work this way. It waits in the background until the given delay has past and then calls the registered ActionListeners actionPerformed method.

Secondly, if it did work this way, it would block the Event Dispatching Thread, preventing it from processing repaint requests.

I think you will find How to use Swing Timers of use.

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

}
于 2013-01-21T10:23:07.220 回答