Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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_Swing_Jtextarea - Fatal编程技术网

Java 如何从其他函数写入JTextArea?

Java 如何从其他函数写入JTextArea?,java,swing,jtextarea,Java,Swing,Jtextarea,我的应用程序就像网络游戏一样工作,我从服务器收到消息,我想在我的JTextArea中显示它们。代码如下所示: public class klient extend JFrame{ ...declarations JTextArea areaText; public klient(){ setSize(600,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("examp

我的应用程序就像网络游戏一样工作,我从服务器收到消息,我想在我的JTextArea中显示它们。代码如下所示:

public class klient extend JFrame{
    ...declarations
    JTextArea areaText;

public klient(){
    setSize(600,300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("example");
    setLocationRelativeTo(null);
    getContentPane().add(createComponents());
}

public JPanel createComponents(){
    JPanel mainPanel = ...
    ....
    Jpanel games = ....

    areaText = new JTextArea(...);
    areaText.setFont...
    ....
    areaText.setEditable(false);

    games.add(new JScrollPane(areaText, JscrollPane.V..., JScrollPane.H...));
}
}
然后我有经典的主函数

public static void main (String[] args){
    ....
    klient okno = new klient();
    ....
    line = reader.readLine();
}

我是否能够以某种方式将行中的字符串添加到JTextArea区域文本中

应该是这样的

okno.getAreaText().append(line);

其中getAreaText()方法返回类的areaText字段。

是,就是这样。我工作了一整天,一整夜,都想不出来:D谢谢