[英]Java GUI - How to append text to JTextArea from a static method?
[英]How to append text to JTextArea from main function int java?
我想从main函数向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");
}
}
我正在用“BBBB”获得textarea。
UPDATE
我还有一个功能。 我从init()
调用它,文本正在追加,一切都很好。 但是如果我把一行setTextArea("some text");
后行clientSocket = new Socket(address, port);
,文字不会附加。
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);
}
}
您将“BBBB”附加到文本区域,因为init
方法用作applets
和servlets
的入口点。
您的类extends JApplet
,它是java.applet.Applet
的子类,这意味着它将使用init
而不是main
(它用作应用程序的入口点)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.