在gui Java中为if语句添加延迟显示输出

时间:2013-03-16 09:12:44

标签: java swing jlabel visible

我制作了这个简单的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Nice extends JFrame implements ActionListener{
  JLabel n1 = new JLabel("1");
  JLabel n2 = new JLabel("2");
  JLabel n3 = new JLabel("3");
  JLabel n4 = new JLabel("4");
  JLabel n5 = new JLabel("5");
  JButton show = new JButton("Show The Numbers");

{
    n1.setVisible(false);
    n2.setVisible(false);
    n3.setVisible(false);
    n4.setVisible(false);
    n5.setVisible(false);
}

public Nice(){
super("Timer");
setVisible(true);
setSize(200, 200);
setLayout(new GridLayout(3,3));
add(n1);
add(n2);         
add(n3);
add(n4);
add(n5);
add(show);
show.addActionListener(this);
   }

public void actionPerformed(ActionEvent a){
  Object clicked = a.getSource();
  if (show == clicked)
  {
n1.setVisible(true);
n2.setVisible(true);
n3.setVisible(true);
n4.setVisible(true);
n5.setVisible(true);
}
}
}

还有一堂课要读它

public class NiceOpener{
  public static void main(String[] args){
    Nice frame = new Nice();
    frame.setVisible(true);
  }
}

但是,我想要做的是点击显示按钮后,而不是一次设置所有数字,它会计算2秒,然后再显示另一个数字。这就像当我点击Show Me它会显示1然后2秒后会显示2然后3秒后会显示3依此类推。什么是最简单的延迟标签可见设置的方法?

3 个答案:

答案 0 :(得分:1)

    public void actionPerformed(ActionEvent a) {
    Object clicked = a.getSource();
    if (show == clicked) {
        new ShowTask().start();
    }
}

class ShowTask extends Thread {
    @Override
    public void run() {
        int i = 2;
        n1.setVisible(true);
        try {
            Thread.sleep((i++) * 1000);
            n2.setVisible(true);
            Thread.sleep((i++) * 1000);
            n3.setVisible(true);
            Thread.sleep((i++) * 1000);
            n4.setVisible(true);
            Thread.sleep((i++) * 1000);
            n5.setVisible(true);
        } catch (InterruptedException e) {
        }

    }
}

答案 1 :(得分:1)

您可以使用javax.swing.Timer更新

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Nice extends JFrame implements ActionListener{
  JLabel n1 = new JLabel("1");
  JLabel n2 = new JLabel("2");
  JLabel n3 = new JLabel("3");
  JLabel n4 = new JLabel("4");
  JLabel n5 = new JLabel("5");
  JButton show = new JButton("Show The Numbers");
  int counter = 1 ;
  boolean started = false;
  javax.swing.Timer timer ;
{
    n1.setVisible(false);
    n2.setVisible(false);
    n3.setVisible(false);
    n4.setVisible(false);
    n5.setVisible(false);
}

public Nice(){
super("Timer");
timer = new javax.swing.Timer(2000,new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent evt)
        {
            hideAllLabels();
            if (counter == 1)
            {
                n1.setVisible(true);
                counter++;
            }
            else if (counter == 2)
            {
                n4.setVisible(true);
                counter++;
            }
            else if (counter == 3)
            {
                n2.setVisible(true);
                counter++;
            }
            else if (counter == 4)
            {
                n5.setVisible(true);
                counter++;
            }
            else if (counter == 5)
            {
                n3.setVisible(true);
                counter++;
                timer.stop();
            }
        }
    });
timer.setInitialDelay(0);
timer.setDelay(2000);
timer.setRepeats(true);
setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200, 200);
setLayout(new GridLayout(3,3));
add(n1);
add(n2);         
add(n3);
add(n4);
add(n5);
add(show);
show.addActionListener(this);
}
private void hideAllLabels()
{
    n1.setVisible(false);
    n2.setVisible(false);
    n3.setVisible(false);
    n4.setVisible(false);
    n5.setVisible(false);
}
public void actionPerformed(ActionEvent a){
  Object clicked = a.getSource();
  if (show == clicked)
  {    
      if (!timer.isRunning())
      {
          if (!started)
          {
            timer.start();
            started = true;
          }
          else
          {
              counter = 1;
              timer.restart();
          }

      }
  }
}
}
public class NiceOpener{
  public static void main(String[] args){
    Nice frame = new Nice();
    frame.setVisible(true);
  }
}

答案 2 :(得分:1)

有很多方法可以达到你想要的效果。

你必须面对许多问题。首先,您必须永远不要在事件调度线程的上下文中执行任何耗时或阻塞操作。除此之外,这样做会阻止Swing处理重绘请求。

第二个是,你必须永远不要在EDT之外的任何线程中创建或修改任何UI组件。

最简单的方法是使用javax.swing.Timer。另一种方法是使用SwingWorker

以下示例提供了一种可以定义伪随机顺序的方法。

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLabelDelay extends JFrame implements ActionListener {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                TestLabelDelay frame = new TestLabelDelay();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    private List<JLabel> labels;

    public TestLabelDelay() {
        super("Timer");
        setLayout(new GridLayout(3, 3));

        labels = new ArrayList<>(25);
        for (int index = 0; index < 5; index++) {
            labels.add(createLabel(index));
        }

        add(labels.get(0));
        add(labels.get(1));
        add(labels.get(2));
        add(labels.get(3));
        add(labels.get(4));

        JButton show = new JButton("Show");
        add(show);
        show.addActionListener(this);
    }

    protected JLabel createLabel(int index) {
        JLabel label = new JLabel(String.valueOf(index));
        label.setVisible(false);
        return label;
    }

    public void actionPerformed(ActionEvent a) {
        List<Integer> order = new ArrayList<>(5);
        order.add(2);
        order.add(4);
        order.add(0);
        order.add(3);
        order.add(1);

        new ShowWorker(labels, order).execute();
    }

    public class ShowWorker extends SwingWorker<Void, Integer> {

        private List<JLabel> labels;
        private List<Integer> order;

        private ShowWorker(List<JLabel> labels, List<Integer> order) {
            this.labels = labels;
            this.order = order;
        }

        @Override
        protected void process(List<Integer> chunks) {
            for (Integer index : chunks) {
                JLabel label = labels.get(order.get(index));
                label.setVisible(true);
                label.repaint();
            }
        }

        @Override
        protected Void doInBackground() throws Exception {
            for (int index = 0; index < labels.size(); index++) {
                Thread.sleep(500);
                publish(index);
            }
            return null;
        }

    }

}