你应该通过
Client到
ServerThread通过构造。在
Client你内实例
run()是不一样的参考
Client你创建
main()。所以你的
ServerThread课就像
ServerThread(Client client, Socket socket, String userName) { this.client = client; this.socket = socket; this.userName = userName; messagesToSend = new linkedList<String>();}public void run() { try { Jtextarea test2 = this.client.gettextarea_Receive(); String test3 = "Hello World"; test2.append(test3); } catch (IOException e) {}}您的
startClient()方法将更新为这样的内容
private void startClient(Client client, Scanner scan){ try { //Create new socket and wait for network communication Socket socket = new Socket(serverHost, serverPort); Thread.sleep(1000); //Create thread and start it ServerThread serverThread = new ServerThread(client, socket, userName); serverAccessThread.run(); }}话虽如此,
我建议将您
main()移出
Client与SWING UI代码不太相关的类。像这样:
public class MySwingApplication { private static final String host = "localhost"; private static final int portNumber = 4444; public static void main(String[] args) { // Requests user to enter name // Start client }}Client然后,您的构建更像实例对象
public class Client extends Jframe { public Jtextarea gettextarea_Receive(){ // Return the text area } // Constructor -- public to allow instantiation from main() public Client(String userName, String host, int portNumber) { // Do stuff } private void startClient(Scanner scan) { // Show the Jframe on screen // Spawn Server }}


