【发布时间】:2012-10-14 17:20:54
【问题描述】:
所以我正在制作的程序使用 2 个线程:一个用于 GUI,一个用于工作。
我希望工作线程/类的更新在 GUI 类的 JTextArea 上打印出来。 我尝试的一切似乎都不起作用。我在将文本添加到 JTextArea 的行之后添加了行以在控制台上打印出文本,以确保它已到达该行,但每次控制台获取文本但 GUI 中的 JTextArea 没有发生任何更改。
public static void consoleText(String consoleUpdate){
GUI.console.append(consoleUpdate);
}
我在工作班上试过这个,但什么也没发生。 有人知道如何解决我的问题吗?
编辑:
MAIN.JAVA
public class main {
public static void main(String[] args) {
Thread t1 = new Thread(new GUI());
t1.start();
}
GUI.JAVA
public class GUI extends JFrame implements Runnable{
public static JTextArea console;
private final static String newline = "\n";
public void run(){
GUI go = new GUI();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(350, 340);
go.setVisible(true);
}
public GUI(){
setLayout(new FlowLayout());
console = new JTextArea(ConsoleContents, 15, 30);
add(console);
}
WORK.JAVA
...{
consoleText("\nI want this text on the JText Area");
}
public static void consoleText(String consoleUpdate){
GUI.console.append(consoleUpdate);
}
【问题讨论】:
-
不知道如何帮助你,为了更好的帮助,请尽快发布SSCCE,简短,可运行,可编译,仅关于
JTextArea#append("String") -
欢迎来到这个论坛,请see FAQ
-
好的,我会尽量截断它以发布
-
@Andy Runnable 只是将一些工作包装在一个类中的一种方式。线程与并行执行直接相关。无论如何,在 Swing 中,所有事情(与 GUI 相关的)都应该在 EDT(事件调度线程)上执行,并且确保您的代码在 EDT 上运行的一种简单技术是将其包装在
Runnable中并将其交给SwingUtilities.invokeLater(Runnable)。这可能会让您认为您需要的是 Runnable 而不是 Thread。 -
这是example。
标签: java swing append jtextarea settext