【发布时间】:2017-07-22 19:39:28
【问题描述】:
我知道如何在控制台中显示线程中的文本,但是如何将它放在 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 + " ");
}
}
}
【问题讨论】:
-
这里有很多问题。我会回答一个:使用
TextA.appendText(String)在TextArea中显示文本 -
嗯,我知道你通常可以附加它的方式。但是我怎样才能把这个字符变成一个字符串呢?因为我不能在 TA 中附加字符。 @IvanPronin
-
我会给你一个提示,以便你找到关于这个主题的数百个答案。 UI 只能从 UI 线程修改
-
要显示来自
Thread的文本,请执行Platform.runLater(TextA.appendText("someString")
标签: java multithreading javafx append