Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.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 在运行时在jtextArea中添加随机单词,而不使用键盘_Java_Random_Jtextarea - Fatal编程技术网

Java 在运行时在jtextArea中添加随机单词,而不使用键盘

Java 在运行时在jtextArea中添加随机单词,而不使用键盘,java,random,jtextarea,Java,Random,Jtextarea,正如标题所说,我想在运行时在jtextArea中添加单词,我只写了以下内容: import java.awt.*; import javax.swing.*; public class Test extends JFrame { private static final long serialVersionUID = 1L; private JTextArea tarea; public Test() { tarea = new JTextArea(

正如标题所说,我想在运行时在jtextArea中添加单词,我只写了以下内容:

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea tarea;

    public Test() {
        tarea = new JTextArea(10, 10);
    }

    private void init() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        insertRandomLetterInsideJtextArea();
        JScrollPane scroll = new JScrollPane(tarea);
        getContentPane().add(scroll, BorderLayout.CENTER);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    private void insertRandomLetterInsideJtextArea() {
        while (true) {
            tarea.setText("foo\n");
        }
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().init();
            }
        });
    }
}
但它不起作用。
while(true)
不允许显示任何内容。
有人可以解释一下原因吗?

它不允许显示任何内容,因为应该初始化所有内容的线程只是在while循环中永远循环,所以它不能进行任何进一步的初始化

您需要在
insertRandomLetterInsideJtextArea
中启动一个线程,如下所示:

private void insertRandomLetterInsideJtextArea() {
        new Thread() {
            Random r = new Random();
            public void run() {
              while (true) {
                try {
                  sleep(1000); // to not kill your app wait a little bit before adding next letter.
                  char c = (char) (r.nextInt(26) + 'a');
                  tarea.setText(tarea.getText() + c);             
                } catch (Exception e) {}
              }
            }
        }.start();
    }

它不允许显示任何内容,因为应该初始化everthing的线程只是在while循环中永远循环,所以它不能进行任何进一步的初始化

您需要在
insertRandomLetterInsideJtextArea
中启动一个线程,如下所示:

private void insertRandomLetterInsideJtextArea() {
        new Thread() {
            Random r = new Random();
            public void run() {
              while (true) {
                try {
                  sleep(1000); // to not kill your app wait a little bit before adding next letter.
                  char c = (char) (r.nextInt(26) + 'a');
                  tarea.setText(tarea.getText() + c);             
                } catch (Exception e) {}
              }
            }
        }.start();
    }

你应该使用定时器来实现这一点。 试试这个:

package test;

import java.awt.BorderLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea tarea;

    public Test() {
        tarea = new JTextArea(10, 10);
    }

    private void init() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scroll = new JScrollPane(tarea);
        getContentPane().add(scroll, BorderLayout.CENTER);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
        insertRandomLetterInsideJtextArea();
    }

    private void insertRandomLetterInsideJtextArea() {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            String content = "";
            @Override
            public void run() {
                content += "foo\n"; // here generate your random String
                tarea.setText(content);
            }
        }, 100, 1000); // firt is time before start, second is duration before repeat task, both in ms

    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().init();
            }
        });
    }
}

你应该使用定时器来实现这一点。 试试这个:

package test;

import java.awt.BorderLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Test extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextArea tarea;

    public Test() {
        tarea = new JTextArea(10, 10);
    }

    private void init() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scroll = new JScrollPane(tarea);
        getContentPane().add(scroll, BorderLayout.CENTER);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
        insertRandomLetterInsideJtextArea();
    }

    private void insertRandomLetterInsideJtextArea() {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            String content = "";
            @Override
            public void run() {
                content += "foo\n"; // here generate your random String
                tarea.setText(content);
            }
        }, 100, 1000); // firt is time before start, second is duration before repeat task, both in ms

    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().init();
            }
        });
    }
}