Java使用MainWindow函数 [英] Java using MainWindow functions

查看:292
本文介绍了Java使用MainWindow函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 MainWindow.java 创建带有所有控件和事物的窗口。我把菜单对象放在窗口上,菜单栏中的选项之一是使程序成为服务器。所以这里的主窗口看起来像这样:

  public class MainWindow extends javax.swing.JFrame {
// all代码包括菜单点击动作处理程序
//Server.start()
}

单击该选项时,它将进入 Server.java 类并启动服务器。这是这个类的骨架:

  public class Server {

public static void start ){
try {
startServer(Integer.parseInt(port));
} catch(Exception e){
e.printStackTrace();
}
}

public static void startServer(int PORT)throws Exception {
...
}

private静态类ClientListenThread extends Thread {

public ClientListenThread(Socket socket,int ClientNumber){
...
}

public void run(){
...
}
}

私人静态类ServerSendThread extends Thread {

public ServerSendThread(Socket socket){
...
}

public void run(){
...
}
}
}

现在的问题是,一旦它进入了 Server 它侦听连接并连接正常,但我只是不能回到 MainWindow 类。它保留在 Server 类中。我甚至不能通过 MainWindow.function()调用 MainWindow 函数,因为它说

$无法从MainWindow类型的非静态方法函数()的静态引用
$ b

  pre> 

我甚至尝试将所有服务器类代码放入MainWindow类或上面,但Java didn 't喜欢,说,它想要在一个单独的文件。



我如何在Server类中引用 MainWindow 函数?

解决方案

您需要创建一个MainWindow类的实例, MainWindow m = new MainWindow()然后调用 m.function()的函数, static。



静态意味着您可以调用函数而不创建对象的实例。这是为什么你得到的错误,因为你的函数不是静态的,所以它需要一个对象的实例被调用。



你还要确保将MainWindow类导入到Server类中。


So I have a MainWindow.java that creates the window with all the controls and things. I put a menubar object on the window, one of the options in the menubar is make the program a server. So here's the main window looks like this:

public class MainWindow extends javax.swing.JFrame {
     //all code including menubar click action handler
     //Server.start()
}

When you click the option, it goes into the Server.java class and starts the server. Here's the skeleton of that class:

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() {
            ...
        }
    }
}

The problem now is that once it gets inside the Server class, it listens for connections and connects fine but I just can't go back to the MainWindow class. It stays within the Server class. I can't even call the MainWindow functions by doing MainWindow.function() because it says

Cannot make a static reference to the non-static method function() from the type MainWindow

I even tried putting all of the Server class code into the MainWindow class or just above it but Java didn't like that and said it wanted it in a separate file.

How exactly do I reference MainWindow functions from within the Server class? Or is there a better way of going about this?

解决方案

You need to either create an instance of the MainWindow class using for instance MainWindow m = new MainWindow() and then calling the function as m.function(), or declare your function as static.

Static means that you can call a function without creating an instance of the object. This is why you get the error, since your function is not static, so it requires an instance of the object to be called.

You'll also want to make sure that the MainWindow class is imported into the Server class.

这篇关于Java使用MainWindow函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆