在用户屏幕上显示延迟文本

时间:2014-10-05 16:00:18

标签: java text dialog


您好!

我正在尝试在屏幕上显示文本(使用Java),但我希望它被延迟,例如,每0.1秒,文本的一个字母将出现在屏幕上。这就像Pokemons对话框。以下是我所说的:https://www.youtube.com/watch?v=yUS1IcC5CBY

我不希望文本的淡入淡出和加速,我只想让文字逐字出现。另外,我希望文本是一个字符串。拜托,你能帮帮我吗?

提前多多感谢!

4 个答案:

答案 0 :(得分:1)

您可以使用两种方法:

一个是Thread.sleep(),如上所示:

private static String message = "Your Message";
private static JLable label = new JLabel();
private static String labelMessage = "";
for(int i = 0; i < message.length(); i++){
    labelMessage += Character.toString(message.charAt(i));
    label.setText(labelMessage);
    try{
        Thread.sleep(howManyMillisecondsYouShouldWait);//if you want to do it every .1
        //seconds, just wait 100 milliseconds.
    }catch(InterruptedException e){
        Thread.currentThread().interrupt();
    }
}

将永远每100毫秒将其打印到屏幕上。然而,使用Thread.sleep的唯一麻烦是(我不知何故只是在前几天才学会这个,即使我已经编程了很长时间)但它并不总是准确的。它可以睡100毫秒,它可能睡150,等等。其次,较慢的计算机可能需要更长的时间才能通过它睡觉。

您将更频繁地使用的另一种方法(可能)是检查系统的实际时间,看看自上次将其打印到屏幕后它是否足够长,如下所示:

private static long timeOfLastWrite;//at what time did you last update the text?
private static long deltaTimeSinceLastWrite;//how long has it been since you last updated the text?
private static long timeOfFirstWrite;//when did you start?
private static long deltaTimeSinceFirstWrite;//how long has it been since you started?
private static String message = "Your Message";
private static JLabel label = new JLabel();
private static String labelMessage = "";
//print once here:
timeOfFirstWrite = System.currentTimeMillis();
timeOfLastWrite = System.currentTimeMillis();//every time you print to the screen, make
//sure that you make note of it by setting the timeOfLastWrite variable equal to the current time.
labelMessage += Character.toString(message.chatAt(0));
while(!labelMessage.equals(message)){
    deltaTimeSinceLastWrite = System.currentTimeMillis() - timeOfLastWrite;
    if(deltaTimeSinceLastWrite >= 100){
        timeOfLastWrite = System.currentTimeMillis();
        deltaTimeSinceFirstWrite = System.currentTimeMillis() - timeOfFirstWrite;
        int currentIndexOfChain = (int) deltaTimeSinceFirstWrite / 100;
        if(currentIndexOfChain >= message.length()){
            currentIndexOfChain = message.length() - 1;
        }
        labelMessage = message.substring(0, currentIndexOfChain + 1);
        label.setText(labelMessage);
    }
}

这个方法对于一个程序来说甚至不是必需的,就像将文本写入屏幕10次一样简单。然而,进入它的实践是很好的。您将了解到,如果您创建一个角色并告诉他移动10个像素,Thread.sleep(100),然后再移动等等......在较慢的计算机上,该角色将移动得更慢。但是,如果你告诉它要等到你的计算机的时间已经过了一定的时间,如果用户退出并且它告诉角色再次移动需要200毫秒,你可以考虑到通过简单地让他移动两次 - 我认为它被称为帧率独立。

如果我对delta时间管理有任何错误,请立即告诉我。同样,我前几天才了解到这一点,即使我已经编程了一段时间,所以如果你现在正在学习编程,不要太担心它。

这就是你如何对一个非常简单的问题做出令人难以置信的长(可能太长)答案。我希望你能从这一回应中受益。

答案 1 :(得分:0)

I'm unable to use Thread.sleep(x) or wait(): java.lang.InterruptedException; must be caught or declared to be thrown

try {
    Thread.sleep(100);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

答案 2 :(得分:0)

您可以使用此代码执行此操作。只需将一个线程打印到jLabel上即可。

             new Thread(new Runnable() {
                @Override
                public void run() {
                    String x="";   
                    String txt="Hello this is a sample text. Let us see how this works.";
                    for(int i=0;i<txt.length();i++){
                        try {

                                jLabel1.setText(x=x+txt.charAt(i));

                                Thread.sleep(100);
                            } catch(InterruptedException ex) {
                                Thread.currentThread().interrupt();
                            }
                    }
                }
            }).start();

答案 3 :(得分:-1)

这个怎么样?

import javax.swing.Timer;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class DelayText
{

   public String example = "As you can see, this sentence is being printed out a character at a time.";
   public String transfer = "";
   public Timer t;
   public int i = 0;
   public JFrame f;
   public JLabel l;

   public DelayText()
   {



      f = new JFrame("Example");   
      l = new JLabel();

      f.add(l);

      f.setSize(450, 200);

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      f.setVisible(true);


      TimerListener tl = new TimerListener();

      t = new Timer(100, tl);

      t.start();




   }

   public static void main(String[] args)
   {

      DelayText d = new DelayText();

   }

   private class TimerListener implements ActionListener
   {

      public void actionPerformed(ActionEvent e)
      {

         if(i < example.length())
         {

            transfer += (example.charAt(i));
            l.setText(transfer);
            i++;



         }

         if(i >= example.length())
         {

            t.stop();

         }



      }

   }

}

我正在使用计时器在JFrame上输出的每个字符之间创建延迟。我注意到其他很多这些都有点复杂,认为这可能会让事情变得更容易理解。

相关问题