简体   繁体   English

如何从其他函数写入JTextArea?

[英]How to write into JTextArea from other functions?

my app works as network game, and I am getting messages from server and I wanna show them in my JTextArea. 我的应用程序充当网络游戏,并且我正在从服务器获取消息,我想在我的JTextArea中显示它们。 Code looks like this: 代码如下:

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...));
}
}

and then I have classic main function 然后我有经典的主要功能

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

Am I able to somehow add String from line into my JTextArea areaText? 我能以某种方式将行中的String添加到我的JTextArea areaText吗?

It should be like this. 应该是这样

okno.getAreaText().append(line);

Where getAreaText() method returns the areaText field of the class. 其中getAreaText()方法返回类的areaText字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM