我知道如何在控制台中显示线程中的文本,但是如何将它放在TextArea中呢?此外,由于我的文本是字符和数字,我怎么能将它们转换为字符串,因为如果我是对的,TextArea只接受字符串?
我需要在TextArea中添加PrintNum,LetterSmall和LetterBig。 有人知道吗?任何帮助将不胜感激!
public class pracice extends Application {
@Override
public void start(Stage primaryStage) {
TextArea ta = new TextArea();
Button btn = new Button();
btn.setText("Show");
btn.setOnAction(new EventHandler<ActionEvent>() {
Runnable run = new PrintNum(25);
Thread th1 = new Thread(run);
char lett;
char lettUp;
Runnable let = new LetterSmall(lett);
Thread th2 = new Thread(let);
Runnable lUp = new LetterBig(lettUp);
Thread th3 = new Thread(lUp);
@Override
public void handle(ActionEvent event) {
System.out.append("\nBegin\n");
th1.start();
try{
th1.join(2000);
} catch (InterruptedException e){
e.printStackTrace();
}
th2.start();
try{
th2.join();
} catch (InterruptedException e){
e.printStackTrace();
}
th3.start();
}
});
BorderPane root = new BorderPane();
root.setTop(btn);
root.setCenter(ta);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Practice");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class PrintNum implements Runnable {
private int lastNum;
Random r = new Random();
public PrintNum(int n){
lastNum = n;
}
public void run(){
System.out.append("\n");
for(int i = 1; i <= lastNum; i++){
int rN = r.nextInt(25) + 1;
System.out.append((lastNum - rN) + " ");
}
}
}
class LetterSmall implements Runnable {
Random r = new Random();
private char lett;
public LetterSmall(char s){
lett = s;
}
public void run(){
System.out.append("\n");
for(int i = 1; i <= 25; i++){
char c = (char) (r.nextInt(26) + 'a');
lett = c;
System.out.append(lett + " ");
}
}
}
class LetterBig implements Runnable {
Random r = new Random();
private char lettUp;
public LetterBig(char up){
lettUp = up;
}
public void run(){
System.out.append("\n");
for(int i = 1; i <= 25; i++){
char c = (char) (r.nextInt(26) + 'A');
lettUp = c;
System.out.append(lettUp + " ");
}
}
}
答案 0 :(得分:-2)
所以我找到了TextArea输入的解决方案。我没有创建三个类线程,而是在主JavaFX类中移动它们。谢谢大家的帮助。
看起来像这样:
new Thread(new Runnable() {
private int lastNum = 25;
private char lett;
private char lettUp;
Random r = new Random();
@Override
public void run() {
Thread th1 = new Thread();
ta.appendText("\n");
for(int i = 1; i <= lastNum; i++){
int rN = r.nextInt(25) + 1;
ta.appendText((lastNum - rN) + " ");
}
Thread th2 = new Thread();
ta.appendText("\n\n");
for(int i = 1; i <= 25; i++){
char c = (char) (r.nextInt(26) + 'a');
lett = c;
ta.appendText(lett + " ");
}
Thread th3 = new Thread();
ta.appendText("\n\n");
for(int i = 1; i <= 25; i++){
char c = (char) (r.nextInt(26) + 'A');
lettUp = c;
ta.appendText(lettUp + " ");
}
th1.start();
try{
th1.join(2000);
} catch (InterruptedException e){
e.printStackTrace();
}
th2.start();
try{
th1.join();
} catch (InterruptedException e){
e.printStackTrace();
}
th3.start();
try{
th1.join();
th2.join();
th3.join();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}).start();