【问题标题】:Non Responsive GUI for java serverjava服务器的非响应式GUI
【发布时间】:2014-12-06 07:01:43
【问题描述】:

我正在尝试为一个小项目创建一个 GUI。这个项目是一个 java 服务器,目前我的代码工作正常,服务器已连接,但它从未退出循环,因此创建并显示了 GUI 的框架,但之后您无法与之交互。我知道循环在哪里,但我似乎无法找到一种方法让我的服务器监听新客户端并且仍然能够与 GUI 交互。这是我的代码:

public class MainServerFrame {

    private static JFrame frame;
    private static ServerSocket serverSocket = null;
    private static Socket clientSocket = null;
    private static final int maxClientsCount = 999;
    private static final clientThread[] threads = new clientThread[maxClientsCount];
    private static StudentStoring studentDB = new StudentStoring();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainServer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void MainServer() {

        // The default port number.
        int portNumber = 3333;
        try {
            serverSocket = new ServerSocket(portNumber);
        } catch (IOException e) {
            System.out.println(e);
        }
        System.out.println("SERVER ONLINE");
        initialize();
        MainServerFrame window = new MainServerFrame();
        MainServerFrame.getFrame().setVisible(true);
        /*
         * Create a client socket for each connection and pass it to a new client
         * thread.
         */

这里是我创建套接字然后进入循环的地方,我可以使用什么样的设计来摆脱这个 while 循环?

        while (true) {
            try {
                clientSocket = serverSocket.accept();
                int i = 0;
                for (i = 0; i < maxClientsCount; i++) {
                    if (threads[i] == null) {
                        (threads[i] = new clientThread(clientSocket, threads, studentDB)).start();
                        System.out.println("client " + i + " is connected");
                        break;
                    }
                }
                if (i == maxClientsCount) {
                    PrintStream os = new PrintStream(clientSocket.getOutputStream());
                    os.println("Server too busy. Try later.");
                    os.close();
                    clientSocket.close();
                }
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    }

    /**
     * Initialize the contents of the frame.
     */
    private static void initialize() {
        setFrame(new JFrame());
        getFrame().setBounds(100, 100, 450, 300);
        getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblServer = new JLabel("SERVER:");
        getFrame().getContentPane().add(lblServer, BorderLayout.NORTH);

    }

    public static JFrame getFrame() {
        return frame;
    }

    public static void setFrame(JFrame frame) {
        MainServerFrame.frame = frame;
    }

}

我的客户

import java.awt.Font;

public class MainFrame implements Runnable {
    //declare Jpanel
    private static JFrame frmHome;
    // The client socket
    private static Socket clientSocket = null;
    // The output stream
    static ObjectOutputStream os;
    // The input stream
    static ObjectInputStream is;
    private static BufferedReader inputLine = null;
    private static boolean closed = false;

    public static void main(String[] args) throws IOException{
        // The default port.
        int portNumber = 3333;
        // The default host.
        String host = "localhost";
        /*
         * Open a socket on a given host and port. Open input and output streams.
         */
        try {
          clientSocket = new Socket(host, portNumber);
          is = new ObjectInputStream(clientSocket.getInputStream());
          os = new ObjectOutputStream(clientSocket.getOutputStream());
          os.flush();
          System.out
          .println("CONNECTED TO SERVER\n"
              + "Now using host=" + host + ", portNumber=" + portNumber);
        } catch (UnknownHostException e) {
          System.err.println("Don't know about host " + host);
        } catch (IOException e) {
          System.err.println("Couldn't get I/O for the connection to the host "
              + host);
        }

        /*
         * If everything has been initialized then we want to write some data to the
         * socket we have opened a connection to on the port portNumber.
         */
        if (clientSocket != null && os != null && is != null) {
          /* Create a thread to read from the server. */
          new Thread(new MainFrame()).start();
        }
      }

      public void run() {
        /*
         * Keep on reading from the socket till we receive "Bye" from the
         * server. Once we received that then we want to break.
         */
          MainFrame window = new MainFrame();
          MainFrame.frmHome.setVisible(true);
        String responseLine;
        try {
          while (!closed) {

          }
        } finally{
            try {
                is.close();
                os.close();
                clientSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        }

    public MainFrame() {
        initialize();
    }

    //function to make window visible
    void setVisible() throws IOException {
        main(null);
    }

    private void initialize() {
        //Initialise Main window with 3 options.
        frmHome = new JFrame();
        frmHome.setTitle("Home");
        frmHome.setBounds(100, 100, 300, 372);
        frmHome.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmHome.getContentPane().setLayout(null);
        frmHome.setResizable(false);

        JLabel lblWelcomeToSrs = new JLabel("Welcome to SRS");
        lblWelcomeToSrs.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblWelcomeToSrs.setBounds(86, 183, 112, 14);
        frmHome.getContentPane().add(lblWelcomeToSrs);

        //initialise all buttons and labels of window.
        JButton btnAdStu = new JButton("Add a student");
        btnAdStu.setBounds(10, 207, 126, 23);
        btnAdStu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                AddStudentFrame adus;
                try {
                    try {
                        adus = new AddStudentFrame();
                        adus.setVisible();
                    } catch (ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    frmHome.setVisible(false);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        });
        frmHome.getContentPane().add(btnAdStu);

        JButton btnCheckStud = new JButton("Search / Modify");
        btnCheckStud.setBounds(146, 207, 127, 23);
        btnCheckStud.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                SearchFrame searchFrame;
                searchFrame = new SearchFrame();
                searchFrame.setVisible();
            }
        });
        frmHome.getContentPane().add(btnCheckStud);

        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(0, 0, 0, 0);
        frmHome.getContentPane().add(lblNewLabel);

        JLabel lblCreatedByRmi = new JLabel("Created by R\u00E9mi Tuyaerts");
        lblCreatedByRmi.setBounds(147, 318, 184, 14);
        frmHome.getContentPane().add(lblCreatedByRmi);  

        JButton btnNewButton = new JButton("Complete List of Students");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                CompleteListFrame studentList = new CompleteListFrame();
                studentList.setVisible();
            }
        });
        btnNewButton.setBounds(52, 241, 184, 23);
        frmHome.getContentPane().add(btnNewButton);

        // wonderful pictures of his excellence design by Yasser
        JLabel lblNewLabel_1 = new JLabel("");
        Image img = new ImageIcon(frmHome.getClass().getResource("/michaelchung.jpg")).getImage();
        lblNewLabel_1.setIcon(new ImageIcon(img));
        lblNewLabel_1.setBounds(80, 11, 120, 148);
        frmHome.getContentPane().add(lblNewLabel_1);
    }
}

【问题讨论】:

    标签: java user-interface server


    【解决方案1】:

    首先我要提到的是在您的方法签名中使用static。它们应该static,并且您应该尝试静态访问它们(MainServerFrame.getFrame().setVisible(true);)。去掉所有方法签名中的static,添加window.getFrame().setVisible(true);等方法的访问权限

    其次,MainServerFrame 正在事件调度线程上创建。 EDT 处理所有 UI 交互。尽管您的代码不完整,但 while 循环似乎在 MainServerFrame 的构造函数中。这样做的问题是while 循环阻塞了事件线程,导致无响应。要修复它,您可以简单地为 while 循环启动一个新线程。

    new Thread(new Runnable(){
        public void run() {
            while (true) {
                ...
            }
        }
    }).start();
    

    此新线程中的任何尝试更新 UI 的调用都应包含在 SwingUtilities.invokeLater 中。类似的东西

    new Thread(new Runnable(){
        public void run() {
            while (true) {
                ...
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                        label.setText(...);
                    }
                });
            }
        }
    }).start();
    

    这是一个简单的重构。为了让示例更简单,我取出了一些代码

    public class MainServerFrame {
    
        private JFrame frame;
        private ServerSocket serverSocket = null;
        private Socket clientSocket = null;
        private int maxClientsCount = 999;
        private JTextArea serverLog = new JTextArea(10, 40);
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MainServerFrame app = new MainServerFrame();
                        app.getFrame().setVisible(true);
                        app.startServer();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        public MainServerFrame() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(serverLog);
            frame.pack();
            frame.setLocationByPlatform(true);
        }
    
        public JFrame getFrame() {
            return frame;
        }
    
        public void startServer() {
             int portNumber = 3333;
            try {
                serverSocket = new ServerSocket(portNumber);
            } catch (IOException e) {
                System.out.println(e);
            }
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        try {
                            clientSocket = serverSocket.accept();
                            final String data = 
                                    (new DataInputStream(clientSocket.getInputStream())).readUTF();
                            SwingUtilities.invokeLater(new Runnable(){
                                public void run() {
                                    serverLog.append(data);
                                    serverLog.append("\n");
                                }
                            });
                        } catch (IOException e) {
                            System.out.println(e);
                        }
                    }
                }
            }).start();
        }
    }
    

    更新:简单客户端

    import java.io.DataOutputStream;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class Cilent {
        public static void main(String[] args) throws Exception {
            Socket socket = new Socket("localhost", 3333);
            Scanner scanner = new Scanner(System.in);
            String data = scanner.nextLine();
            (new DataOutputStream(socket.getOutputStream())).writeUTF(data);
        }
    }
    

    【讨论】:

    • 谢谢,但如果它们不是静态的,我如何从主目录访问它?
    • 我想我已经解释过了。访问实例window
    • 如果您需要将所有内容都设为静态,那么您的设计就有问题。对实例和静态之间的区别进行一些研究。静态不仅仅是能够从主要访问某些东西。有理由让事情变得静态。仅仅能够从主目录访问某些东西,并不足以成为让一切都静止的充分理由。花一些时间重新考虑设计
    • 我按照您的解释做了 tahnks,但在我的 main() 中运行 MainServer() 时遇到问题。我试图在函数之外创建变量窗口以使其可供所有人访问,但它仍然告诉我应该将其设为静态我真的很困惑
    • 看看我的更新。我做了一点重构。你可以从中得到一些想法。我从来没有用过static
    猜你喜欢
    • 2021-04-14
    • 2012-04-20
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2014-06-04
    • 2014-06-25
    相关资源
    最近更新 更多