在不使用键盘的情况下在运行时在 jtextArea 中添加随机词
Add random words at runtime inside jtextArea without using keyboard
正如标题所说,我想在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();
}
您应该使用计时器来实现此目的。
试试这个:
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();
}
});
}
}
正如标题所说,我想在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();
}
您应该使用计时器来实现此目的。 试试这个:
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();
}
});
}
}