【发布时间】:2014-03-25 21:24:17
【问题描述】:
所以我有一个MainWindow.java,它创建了一个包含所有控件和东西的窗口。我在窗口上放了一个菜单栏对象,菜单栏中的选项之一是使程序成为服务器。所以这里的主窗口看起来像这样:
public class MainWindow extends javax.swing.JFrame {
//all code including menubar click action handler
//Server.start()
}
当您单击该选项时,它会进入Server.java 类并启动服务器。这是该类的骨架:
public class Server {
public static void start(String port) {
try {
startServer(Integer.parseInt(port));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void startServer(int PORT) throws Exception {
...
}
private static class ClientListenThread extends Thread {
public ClientListenThread(Socket socket, int ClientNumber){
...
}
public void run() {
...
}
}
private static class ServerSendThread extends Thread {
public ServerSendThread(Socket socket) {
...
}
public void run() {
...
}
}
}
现在的问题是,一旦它进入 Server 类,它会侦听连接并正常连接,但我无法回到 MainWindow 类。它位于Server 类中。我什至不能通过 MainWindow.function() 调用 MainWindow 函数,因为它说
Cannot make a static reference to the non-static method function() from the type MainWindow
我什至尝试将所有 Server 类代码放入 MainWindow 类或就在它上面,但 Java 不喜欢这样,并说它希望将它放在一个单独的文件中。
我究竟如何从 Server 类中引用 MainWindow 函数?还是有更好的方法来解决这个问题?
【问题讨论】:
标签: java swing class sockets tcp