【问题标题】:Delay between lines in Java [closed]Java中的行之间的延迟[关闭]
【发布时间】:2018-06-10 22:42:56
【问题描述】:

我正在用 Java 创建一个纸牌游戏,并想描绘发牌的外观。我创建了 JLabels 并通过使用 .setVisible(true) 使它们可见,并希望在每张可见的卡之间延迟。 我试过使用 wait() 和 Thread.sleep() 但都没有奏效。 wait() 给了我错误,Thread.sleep 只是暂停了整个程序,而不仅仅是每次出现。 (此代码在按下“下一步”按钮时运行,因此它在 actionPerformed 方法中)

注意:我是初学者..请保持简单:)

else if(eventName.equals("Next"))
  {
     rules.setVisible(false);
     next.setVisible(false);
     inst.setVisible(false);
     cardDeck.setVisible(true);
     discard.setVisible(true);
     tot.setVisible(true);
     specialCards.setVisible(true);
     // delay here
     userCard1.setVisible(true);
     // delay here
     compCard1.setVisible(true);
     // delay here
     userCard2.setVisible(true);
     // delay here
     compCard2.setVisible(true);
     // delay here
     userCard3.setVisible(true);
     // delay here
     compCard3.setVisible(true);
  }  

【问题讨论】:

  • 你必须为每个视图设置动画
  • 这最终会在事件线程上执行吗?例如在像 here 这样的 ActionListener 中
  • 不是在动作监听器中,而是在actionPerformed方法中
  • 那不作为ActionListener接口中定义的actionPerformed使用???
  • 好吧,你不应该在 Event Dispatch Thread 上使用 sleep (block) - 这会阻塞 GUI,也就是说,没有更新...试试 SwingWorker(在 StackOverflow 上搜索,这是一个很好的已知问题...但我现在搜索它为时已晚,抱歉)

标签: java swing delay wait


【解决方案1】:

使用Timers 让标签在一段时间后显示。 您将在文档中看到使用它们非常简单。

您也可以像Thread.sleep 那样插入让您的应用暂停一段时间的代码,但这会使您的应用在等待期间完全无响应。

简单示例:

package testTimer;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TimerTest extends JFrame {

    // These labels will be shown after some delay
    JLabel l1 = null;
    JLabel l2 = null;

    public TimerTest() {

        Container cpane = getContentPane();
        cpane.setLayout(new BoxLayout(cpane, BoxLayout.Y_AXIS));
        JLabel l = new JLabel("Timer Test");
        l.setAlignmentX(Component.CENTER_ALIGNMENT);
        cpane.add(l);
        l.setVisible(true);
        l1 = new JLabel("1");
        l2 = new JLabel("2");
        cpane.add(l1);
        l1.setVisible(false);
        cpane.add(l2);
        l2.setVisible(false);

        // Now prepare two timers, one for each label
        int delayForL1Millis = 1000;
        Timer t1 = new Timer(delayForL1Millis, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                l1.setVisible(true);
            }
        });
        t1.setRepeats(false);

        int delayForL2Millis = 2000;
        Timer t2 = new Timer(delayForL2Millis, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                l2.setVisible(true);
            };
        });
        t2.setRepeats(false);

        // And start them
        t1.start();
        t1 = null;
        t2.start();
        t2 = null;

    }

    public static void main(String[] argv) {

        TimerTest timertest = new TimerTest();
        timertest.setVisible(true);

    }

}

【讨论】:

  • 我试过了,但我不知道如何工作
  • 我添加了一个关于如何使用计时器来显示延迟标签的示例...
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 2022-11-10
  • 1970-01-01
相关资源
最近更新 更多