[英]How to append text to JTextArea from main function int java?
I want to append some text to JTextArea
from main function, but it doesn't work. 我想从main函数向
JTextArea
附加一些文本,但它不起作用。
I am appending text from init()
and from main()
, but only text from init()
appears on JTextArea
. 我从
init()
和main()
附加文本,但是只有来自init()
文本出现在JTextArea
。
public class Test extends JApplet{
private static JPanel panel = new JPanel();
private static JTextArea textArea = new JTextArea();
public void init() {
panel.setLayout(null);
panel.setPreferredSize(new Dimension(400,300));
this.add(panel);
textArea.setBounds(20, 150, 350, 100);
panel.add(textArea);
setTextArea("BBBB");
}
public static void setTextArea(String text){
textArea.append(text);
}
public static void main(String args[]) {
setTextArea("AAAAA");
}
}
I'm getting textarea just with "BBBB". 我正在用“BBBB”获得textarea。
UPDATE UPDATE
I have one more function. 我还有一个功能。 I am calling it from
init()
, text is appending and everything is fine. 我从
init()
调用它,文本正在追加,一切都很好。 But if I put a line setTextArea("some text");
但是如果我把一行
setTextArea("some text");
after line clientSocket = new Socket(address, port);
后行
clientSocket = new Socket(address, port);
, text won't append. ,文字不会附加。
private static void connetToServer() throws IOException, InterruptedException {
try {
//address = args.length > 0 ? args[0] : "localhost";
//port = args.length > 1 ? Integer.parseInt(args[1]) : 4444;
//System.out.println(address + ' ' + port);
setTextArea("some text");
clientSocket = new Socket(address, port);
output = new PrintStream(clientSocket.getOutputStream());
input = new DataInputStream(clientSocket.getInputStream());
inputLine = new DataInputStream(new BufferedInputStream(System.in));
}
catch( IOException e){
setTextArea("Can't connet to server");
System.exit(0);
}
}
You're getting "BBBB" appended to your text area because the init
method is used as an entry point for applets
and servlets
. 您将“BBBB”附加到文本区域,因为
init
方法用作applets
和servlets
的入口点。
Your class extends JApplet
which is a subclass of java.applet.Applet
meaning it will use init
and not main
(which is instead used as an entry point for applications). 您的类
extends JApplet
,它是java.applet.Applet
的子类,这意味着它将使用init
而不是main
(它用作应用程序的入口点)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.