JFrame不会为多个客户端加载

时间:2016-10-07 21:56:07

标签: java swing sockets serversocket

我无法为多个客户端加载JFrame,但它适用于单个客户端。我能够在单个客户端上执行所有操作。框架变得可见,但内容不是。

我需要额外添加一些东西吗?我正在建立正确的连接吗?我在本地机器上运行它。

我可以在同一台机器上运行多个实例吗?

服务器:

public class ServerThread extends Thread{
    public static void main(String[] args) throws IOException {
        EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(4518);
                while(true){
                Socket clientSocket = serverSocket.accept();
                Thread window=new Frame1();
                window.start();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

客户端:

    public class Client extends JFrame{
    public static void main(String arg[]){
        String hostName="localhost";
        int portNumber=4518;
       try{
            Socket echoSocket = new Socket(hostName, portNumber);
            PrintWriter out =
                new PrintWriter(echoSocket.getOutputStream(), true);
            BufferedReader in =
                new BufferedReader(
                    new InputStreamReader(echoSocket.getInputStream()));

        }

的JFrame:

public class Frame1 extends Thread{

    static JFrame frame;
    private static JTextField textField;
    private static JPasswordField passwordField;
    static Connection conn = null;
    static PreparedStatement pst = null;
    static ResultSet rs = null;
public Frame1() {
        System.out.println("hello");
        initialize();
    }
private static void initialize() {
        frame = new JFrame();
        frame.setVisible(true);
        frame.setTitle("Course Registration- Spring 2017");
        frame.getContentPane().setBackground(new Color(204, 204, 255));
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("Sign in");
        btnNewButton.setBackground(new Color(204, 102, 153));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                conn = DBConnect.DBConnect();
                String sql = "Select * from users where username =? and password=?";
                try{
                    String userName=textField.getText();
                    String password=passwordField.getText();
                    pst = conn.prepareStatement(sql);
                    pst.setString(1, userName);
                    pst.setString(2, password);
                    rs = pst.executeQuery();

                    if(userName.equals("admin") && password.equals("admin")){
                        ServerSideViews s = new ServerSideViews();
                        s.setVisible(true);
                        s.setUserName(userName);
                        s.setFrameLogin(frame);
                        System.out.println(userName);
                        frame.setVisible(false);
                    }

                    else if(rs.next() && !(userName.equals("admin"))){
                        JOptionPane.showMessageDialog(null, "Welcome " + textField.getText() );
                        CourseRegistration c = new CourseRegistration();
                        c.setVisible(true);
                        c.setFrameLogin(frame);
                        frame.setVisible(false);
                        String sql1 = "Select major from users where username=?;";
                        System.out.println(sql1);
                        pst = conn.prepareStatement(sql1);
                        pst.setString(1, userName);
                        rs = pst.executeQuery();
                        String major="";
                        while(rs.next())
                        {
                        major=rs.getString("major");
                        }
                        c.setUserName(userName);
                        c.comboBox.setSelectedItem(major);
                        c.comboBox.disable();
                    }
                    else{
                        JOptionPane.showMessageDialog(null, "Invalid user name or password");           
                    }
                }catch(Exception e1){
                        e1.printStackTrace();
                }   
            }
        });


        btnNewButton.setBounds(157, 182, 117, 29);
        frame.getContentPane().add(btnNewButton);

        JLabel lblUserName = new JLabel("User Name");
        lblUserName.setBounds(76, 96, 86, 16);
        frame.getContentPane().add(lblUserName);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(86, 129, 61, 16);
        frame.getContentPane().add(lblPassword);

        textField = new JTextField();
        textField.setBounds(157, 91, 130, 26);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JButton btnNewButton_1 = new JButton("Register");
        btnNewButton_1.setBackground(new Color(51, 102, 204));
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UserRegistration reg = new UserRegistration();  
                reg.setVisible(true);
                reg.setFrameLogin(frame);
                frame.setVisible(false);
            }
        });
        btnNewButton_1.setBounds(157, 225, 117, 29);
        frame.getContentPane().add(btnNewButton_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(157, 124, 130, 26);
        frame.getContentPane().add(passwordField);

        JLabel lblNewLabel = new JLabel("Course Registration - Spring 2017");
        lblNewLabel.setBounds(109, 24, 241, 16);
        frame.getContentPane().add(lblNewLabel);
    }
}

1 个答案:

答案 0 :(得分:1)

不要启动阻止事件调度线程(EDT)的任务。 GUI无法响应事件。

所以不要从SwingUtilities.invokeLater(..)启动ServerSocket。

相反,您需要为ServerSocket使用单独的线程。

阅读Concurrency上Swing教程中的部分,了解更多信息和示例。