Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
628 views
in Technique[技术] by (71.8m points)

java - Firing delay between JFrame components

I want to show how merge sort perform visually using JFrame. What I want to do is to make visible subsequent JLabel with some time delay. I tried many way but all of them appears at same moment with no intermediate delay.

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

But when I click button all the lables appear at same momenet though I have kept delay.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to update the icons in the timer's action listener, as shown here. You can implement the Icon interface to render icons having a size proportional to an element's comparative value, as shown here.

Addendum: Can you please be little bit specific?

You want to animate the intermediate steps of sorting a List<Number> of size N in some initially random order. Number subclasses implement Comparable<T>, so compareTo() is already done. A GridLayout(1, 0) of JLabel each having an Icon can be used to display the values. DecRenderer shows how to create icons with a proportional size; you'll want to vary the height over the interval [0, N). GrayIcons & Mad's example show how to animate the display of the icons in some order.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...