java - 在 gui Java 中对 if 语句的输出显示添加延迟

标签 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,依此类推。延迟标签可见设置的最简单方法是什么?

最佳答案

您可以使用 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);
  }
}

关于java - 在 gui Java 中对 if 语句的输出显示添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447532/

相关文章:

java AWT native 操作系统库?

java - 我的 Swing 组件图形未更新

java - JLabel 不会改变颜色两次

java - JSF/Tobago Facet 列表?

java - ConcurrentSkipListSet 和重新排序 (java)

java - 将 JLabel 添加到面板 - 如何正确布局组件?

java - 垂直 JScrollPane 切断了它的 child 的右侧

java - JLabel文本位置

java - 如何断言数组中某个值的存在

java - 过滤消息正文,我强制关闭