Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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_Eclipse - Fatal编程技术网

Java 在字符间隔上追加文本

Java 在字符间隔上追加文本,java,eclipse,Java,Eclipse,我试图编写一个程序,参与者通过控制台与程序(I/O)通信。诀窍是,控制台是GUI的一部分,因为我需要程序运行一个可执行的jar文件。我用一个可滚动的文本字段附加文本,如下所示 textArea.append(printChar); 我给这个方法一个字符串,它使用一个嵌套的for循环逐个字符地获取它,并附加每个字符(使用String.substring()) 我的问题是,它冻结了它应该打印的全部时间,然后只显示所有内容。我不知道为什么,因为我使用System.out.print对它进行了测试,结

我试图编写一个程序,参与者通过控制台与程序(I/O)通信。诀窍是,控制台是GUI的一部分,因为我需要程序运行一个可执行的jar文件。我用一个可滚动的文本字段附加文本,如下所示

textArea.append(printChar);
我给这个方法一个
字符串
,它使用一个嵌套的for循环逐个字符地获取它,并附加每个字符(使用
String.substring()

我的问题是,它冻结了它应该打印的全部时间,然后只显示所有内容。我不知道为什么,因为我使用
System.out.print
对它进行了测试,结果完全符合我的要求。因此,附加和打印有些不同。有什么想法吗

另外,我正在使用
Thread.Sleep(100)
作为等待时间

public void actionPerformed(ActionEvent evt) {
        if (!preforming){

        preforming = true;
        String input = textField.getText();   //Text from Input
        textArea.append(dungeon.name + ": " + input + newline);     //Add "text" to bottom of console
        String[] output = dungeon.action(input);


        //print everything in array output, char by char, with 2-3 seconds after each
        for (int i = 0; i < output.length; i++){
            String printThis = output[i];

            if (printThis.length() > 0){
                for (int j = 0; j < printThis.length(); j++){
                    String printChar = printThis.substring(j, j+1);
                    textArea.append(printChar);
                    //System.out.print(printChar);
                    try{  
                        Thread.sleep(5);    
                        } catch (InterruptedException e) {
                            System.out.print("Error ");
                    }
                     /*try { //useless
                        Thread.sleep(200);
                     } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                     }*/
                }
            }
             textArea.append("" + newline); 
        }


        //cleaning up input bar
        textField.setText("");
        textField.selectAll();
        //Make sure the new text is visible, even if there
        //was a selection in the text area.
        textArea.setCaretPosition(textArea.getDocument().getLength());
        preforming = false;
        }
    }
public void actionPerformed(ActionEvent evt){
如果(!预成型){
预成型=真;
String input=textField.getText();//来自输入的文本
textArea.append(dungeon.name+“:“+input+newline);//将“text”添加到控制台底部
字符串[]输出=dungeon.action(输入);
//打印数组输出中的所有内容,逐字符打印,每次打印后2-3秒
for(int i=0;i0){
对于(int j=0;j
当您显示更多代码时,我已编辑了我的答案。因为代码中有一个外循环,所以我在这个新编辑中把它包含在
timer
的run方法中。而且我没有地下城的代码,所以我只是暂时用常量值替换它,这样程序就可以在我的测试中运行

public void actionPerformed(ActionEvent evt) {
    java.util.Timer timer = new java.util.Timer();
    timer.schedule(new java.util.TimerTask() {
        public void run() {
            if (!preforming){
                preforming = true;
                String newline = "\n";
                String dungeonName = "Star Light";


                String input = textField.getText();   //Text from Input
                textArea.append(dungeonName + ": " + input + newline);     //Add "text" to bottom of console
                String[] output = {
                    "Twinkle twinkle little star.",
                    "How I wonder what you are.",
                    "Up above the world so high."
                };


                //print everything in array output, char by char, with 2-3 seconds after each
                for (int i = 0; i < output.length; i++){
                    String printThis = output[i];

                    if (printThis.length() > 0){
                        for (int j = 0; j < printThis.length(); j++){
                            String printChar = printThis.substring(j, j+1);
                            textArea.append(printChar);
                            //System.out.print(printChar);
                            try{
                                Thread.sleep(25);
                            } catch (InterruptedException e) {
                                System.out.print("Error ");
                            }
                 /*try { //useless
                    Thread.sleep(200);
                 } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                 }*/
                        }
                    }
                    textArea.append("" + newline);
                }


                //cleaning up input bar
                textField.setText("");
                textField.selectAll();
                //Make sure the new text is visible, even if there
                //was a selection in the text area.
                textArea.setCaretPosition(textArea.getDocument().getLength());
                preforming = false;
            }
        }
    }, 1);
}
public void actionPerformed(ActionEvent evt){
java.util.Timer Timer=新的java.util.Timer();
schedule(新的java.util.TimerTask(){
公开募捐{
如果(!预成型){
预成型=真;
字符串换行符=“\n”;
String dungeonName=“星光”;
String input=textField.getText();//来自输入的文本
textArea.append(dungeonName+“:“+input+newline);//将“text”添加到控制台底部
字符串[]输出={
“闪烁的小星星。”,
“我真想知道你是什么。”,
“高出世界那么高。”
};
//打印数组输出中的所有内容,逐字符打印,每次打印后2-3秒
for(int i=0;i0){
对于(int j=0;j
当您显示更多代码时,我已编辑了我的答案。因为代码中有一个外循环,所以我在这个新编辑中把它包含在
timer
的run方法中。而且我没有地下城的代码,所以我只是暂时用常量值替换它,这样程序就可以在我的测试中运行

public void actionPerformed(ActionEvent evt) {
    java.util.Timer timer = new java.util.Timer();
    timer.schedule(new java.util.TimerTask() {
        public void run() {
            if (!preforming){
                preforming = true;
                String newline = "\n";
                String dungeonName = "Star Light";


                String input = textField.getText();   //Text from Input
                textArea.append(dungeonName + ": " + input + newline);     //Add "text" to bottom of console
                String[] output = {
                    "Twinkle twinkle little star.",
                    "How I wonder what you are.",
                    "Up above the world so high."
                };


                //print everything in array output, char by char, with 2-3 seconds after each
                for (int i = 0; i < output.length; i++){
                    String printThis = output[i];

                    if (printThis.length() > 0){
                        for (int j = 0; j < printThis.length(); j++){
                            String printChar = printThis.substring(j, j+1);
                            textArea.append(printChar);
                            //System.out.print(printChar);
                            try{
                                Thread.sleep(25);
                            } catch (InterruptedException e) {
                                System.out.print("Error ");
                            }
                 /*try { //useless
                    Thread.sleep(200);
                 } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                 }*/
                        }
                    }
                    textArea.append("" + newline);
                }


                //cleaning up input bar
                textField.setText("");
                textField.selectAll();
                //Make sure the new text is visible, even if there
                //was a selection in the text area.
                textArea.setCaretPosition(textArea.getDocument().getLength());
                preforming = false;
            }
        }
    }, 1);
}
public void actionPerformed(ActionEvent evt){
java.util.Timer Timer=新的java.util.Timer();
schedule(新的java.util.TimerTask(){
公开募捐{
如果(!预成型){
预成型=真;
字符串换行符=“\n”;
String dungeonName=“星光”;
String input=textField.getText();//来自输入的文本
textArea.append(dungeonName+“:“+input+newline);//将“text”添加到控制台底部
字符串[]输出={
“眨眨眨