Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在用户屏幕上显示延迟文本_Java_Text_Dialog - Fatal编程技术网

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

Java 在用户屏幕上显示延迟文本,java,text,dialog,Java,Text,Dialog,你好! 我试图用Java在屏幕上显示一个文本,但我希望它被延迟,比如,每0.1秒,屏幕上就会出现一个文本字母。就像口袋妖怪的对话。下面是我要说的: 我不想要文本的淡入淡出和加速,我只想要文本逐字出现。另外,我希望文本是一个字符串。拜托,你能帮我吗 提前多谢 您可以使用此代码来执行此操作。只需将一个线程打印到jLabel上即可 new Thread(new Runnable() { @Override pub

你好!

我试图用Java在屏幕上显示一个文本,但我希望它被延迟,比如,每0.1秒,屏幕上就会出现一个文本字母。就像口袋妖怪的对话。下面是我要说的:

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

提前多谢


您可以使用此代码来执行此操作。只需将一个线程打印到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();

您可以使用两种方法:

一个是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();
    }
}
对于一个简单到每秒向屏幕写入10次文本的程序来说,这种方法甚至没有一点必要。然而,实践它是很好的。您将了解到,如果您创建一个角色,并告诉他移动10像素,Thread.sleep100,然后再次移动,等等。。。在速度较慢的计算机上,角色将移动较慢。但是,如果你让它根据你的计算机时间等待一定的时间,如果用户延迟,需要200毫秒才能告诉角色再次移动,你可以通过简单地让他移动两倍的距离来解释这一点——我认为这叫做帧率独立

如果我在delta时间管理方面有任何错误,请立即告诉我。再说一次,我前几天才知道这一点,尽管我已经编程一段时间了,所以如果你现在正在学习编程,不要太担心

这就是为什么你要对一个非常简单的问题给出一个非常长的答案。我希望您能从这个回复中受益。

这个怎么样

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上输出的每个字符之间创建一个延迟。我注意到很多其他的字母有点复杂,我想这可能会让事情更容易理解。

好的,但是如何延迟文本?使用这个来延迟输出字母之间的时间。您需要使用类似system.out.print的东西;输出文本。您可以使用string.charAtx;一次获取1个字符不,不是System.out.print。。。这仅在控制台上显示。我希望文本是JLabel:Plabel.setTextstring.substring0,x;不不不,你不明白。。。啊。我放弃。--下次请使用更好的youtube视频,或者至少跳到你想复制的视频点。哦。是 啊很抱歉。没问题,只是那家伙10秒钟后开始对着麦克风尖叫,吓了我一跳。所以我做的是花了多长时间,程序第一次启动,然后除以100,得到从那以后已经过了几百毫秒。然后,我取这个数字,再加上一个,因为我们从一个字符开始,得到消息的子字符串,我们希望标签最终等于从0开始,从开始/100开始一直到这个数字加上1。我用System.out.println进行了应用测试,效果很好。哇!非常感谢您提供了这个非常详细和巨大的答案!我将试用这些代码,并告诉您我的问题是否已解决哈哈哈,写得很好!但这听起来肯定是OP在理解线程等简单函数的基础上遇到了困难,像这样一个困难的答案通常不是很有帮助,不管它的有效性或功能性如何。至少,当给出这样的答案时,最好是解释你所做的一切。哈哈,是的,我写了第一部分的主题。睡眠,然后决定尝试以首选的方式教学。我甚至故意用粗体字表示,要完成如此简单的事情,第二种方法绝对是不必要的。。。同时,对我来说,用以下方式编写deltaTime计算器是一种很好的做法:D.@user3658616,当你说它不起作用时,你试过第一种方式吗?它所做的只是循环遍历for循环中消息的每个字符,并将其添加到labelMessage中,然后在这两者之间睡觉。当然,每次你都必须将标签文本设置为相等。哦,我想我知道你做了什么。。。我发布的代码不是全部,只是概念性的。您仍然需要自己创建JFrame并向其中添加JLabel。只是想弄清楚,太棒了!这太完美了!非常感谢!!:D:D:D@user3658616你理解这个例子中发生的一切吗?如果你什么都不懂,我可以解释
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);
    }
}
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();

         }



      }

   }

}