我想从 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”获取文本区域。
更新
我还有一项功能。我从 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
方法用作 applet
和 servlet
的入口点.
您的类扩展了 JApplet
,它是 java.applet.Applet
的子类,这意味着它将使用 init
而不是 main
(改为用作应用程序的入口点)。
关于java - 如何从主要函数 int java 将文本附加到 JTextArea?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177468/