使用套接字循环时 Java Fx 无法正常工作

我对 java fx 有一些问题,我正在开发一个服务邮件,我在显示服务器阶段(使用 java fx)时遇到了问题。通过使用我创建的模式 MVC:1)MainServer.java(我启动 java fx 应用程序的地方)2)ControllerServer.java(控制器类)3)ServerLog.java(模型类,对我的问题并不重要,但不得不提及它)。


现在,我有另一个名为 Server.java 的类,其中有真正的服务器代码,我尝试通过将 Server.java 代码移动到 ControllerServer.java 来将 MainServer.java 与 Server.java 合并,服务器运行良好,但阶段没有显示阶段,我认为问题出在可初始化的while循环中。



public class Server {

    private static Object lock = new Object();

    private static ServerLog serverLog = new ServerLog();


    public static void getService(Socket s) throws IOException {

         // not important for my question so deleted

    }


    public static void main(String[] args){

        new File("users").mkdir();

        try {

            ServerSocket socket = new ServerSocket(8189);


                while(true) {

                    Socket s = socket.accept();

                    getService(s);

                }


        }catch(IOException e){e.printStackTrace();}


    }

}

public class ServerMain extends Application {



    public static void main(String[] args) {

        launch(args);

    }


    @Override

    public void start(Stage primaryStage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("View/server.fxml"));

        primaryStage.setScene(new Scene(root, 800, 600));

        primaryStage.setTitle("Server Log");

        primaryStage.show();

    }

}


public class ControllerServer implements Initializable {


    @FXML

    private ListView myListView;



    //protected List<String> listLog = new ArrayList<>();

    protected ListProperty<String> listProperty = new SimpleListProperty<>();

    private ServerLog serverLog = new ServerLog();



我想要做的是删除 ServerMain.java 并将 ServerMain.java 正在做的事情转移到服务器中(所以服务器需要在打开时显示舞台)


MMTTMM
浏览 109回答 1
1回答

jeck猫

不得不编辑我的答案,因为最后它工作了,但不正确,现在有了这个类,我可以正确看到 java fx 阶段,但服务器停止正常工作,所以如果有人有更好的解决方案,我将很荣幸阅读这个解决方案 :( .public class Server extends Application {&nbsp; &nbsp; private static Object lock = new Object();&nbsp; &nbsp; private static ServerLog serverLog = new ServerLog();&nbsp; &nbsp; public static void getService(Socket s) throws IOException { ... }&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; launch(args);&nbsp; &nbsp; &nbsp; &nbsp; new File("users").mkdir();&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServerSocket socket = new ServerSocket(8189);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Socket s = socket.accept();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getService(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }catch(IOException e){e.printStackTrace();}&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start(Stage primaryStage) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Parent root = FXMLLoader.load(getClass().getResource("View/server.fxml"));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setScene(new Scene(root, 800, 600));&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.setTitle("Server Log");&nbsp; &nbsp; &nbsp; &nbsp; primaryStage.show();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java