【发布时间】:2015-12-09 11:17:16
【问题描述】:
我想在主函数中将一些文本附加到JTextArea,但它不起作用。
我正在附加来自init() 和main() 的文本,但JTextArea 上只有来自init() 的文本。
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。
更新
我还有一个功能。我从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);
}
}
【问题讨论】: