gpt4 book ai didi

java - 使用另一个类中的 JFrame

转载 作者:行者123 更新时间:2023-12-02 02:16:52 25 4
gpt4 key购买 nike

嗨,我在尝试将同一包中另一个类的一些文本附加到 JTextArea 时遇到了一个小问题。下面是与 JFrame 相关的主类:

  public class Client extends JFrame{

//Static variables
private static final String host = "localhost";
private static final int portNumber = 4444;

//Variables
private String userName;

//JFrame Variables
private JPanel contentPanel;
private JTabbedPane panel_Social;
private JPanel jpanel_Social;
private JPanel jpanel_Chat;
private JTextArea textArea_Receive;
private JTextField textField_Send;
private JTextArea textArea_ClientList;
private JButton btn_Enter;


public JTextArea getTextArea_Receive(){
return this.textArea_Receive;
}

//Constructor
private Client(String userName, String host, int portNumber){
this.userName = userName;
this.serverHost = host;
this.serverPort = portNumber;
}

public void main(String args[]){
//Requests user to enter name
String readName = null;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter username");
readName = scan.nextLine();

//Start client
Client client = new Client(readName, host, portNumber);
client.startClient(scan);
}

private void startClient(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 = new ServerThread(socket, userName);
Thread serverAccessThread = new Thread(serverThread);
serverAccessThread.start();
}
}
}


Below is the serverThread class

public class ServerThread implements Runnable{

private Socket socket;
private String userName;
private boolean isAlived;
private final LinkedList<String> messagesToSend;
private boolean hasMessages = false;

//Constructor
ServerThread(Socket socket, String userName){
this.socket = socket;
this.userName = userName;
messagesToSend = new LinkedList<String>();
}

public void run(){
try{
Client test1 = new Client();
JTextArea test2 = test1.getTextArea_Receive();
String test3 = "Hello World";
test2.append(test3);
} catch (IOException e){
}
}

我包含测试变量只是为了轻松重现问题,但每当运行追加函数时,jFrame 的文本区域中不会出现任何内容。在我的场景中,我让客户端从服务器接收文本,然后将其附加到文本框。

顺便说一句,我正在为 JFrame 使用 IntelliJ GUI 设计器。我只包含了重现问题所需的代码。我仍在尝试创建 MCVE 问题,因此请随时让我知道我犯的错误。

最佳答案

您应该通过构造函数将 Client 传递到 ServerThread 中。您在 run() 中实例化的 Client 与您在 main() 中创建的 Client 的引用不同>。所以你的 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
}
}

关于java - 使用另一个类中的 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49178828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com