public Server() {  
        frame = new JFrame("伺服器");  
        // 更改JFrame的图标：  
        //frame.setIconImage(Toolkit.getDefaultToolkit().createImage(Client.class.getResource("qq.png")));  
        //frame.setIconImage(Toolkit.getDefaultToolkit().createImage(Server.class.getResource("qq.png")));  
        contentArea = new JTextArea();  
        contentArea.setEditable(false);  
        contentArea.setForeground(Color.red);  
        txt_message = new JTextField();  
        txt_max = new JTextField("30");  
        txt_port = new JTextField("5566");  
        btn_start = new JButton("啟動");  
        btn_stop = new JButton("終止");  
        btn_send = new JButton("send");  
        btn_stop.setEnabled(false);  
        listModel = new DefaultListModel();  
        listmodel = new DefaultListModel();
        userList = new JList(listModel);
        roomList = new JList(listmodel);
  
        southPanel = new JPanel(new BorderLayout());  
        southPanel.setBorder(new TitledBorder("公告"));  
        southPanel.add(txt_message, "Center");  
        southPanel.add(btn_send, "East");
        westPanel = new JPanel(new BorderLayout());
        leftpanel = new JScrollPane(roomList);
        leftpanel.setBorder(new TitledBorder("房間列表"));
        leftPanel = new JScrollPane(userList);  
        leftPanel.setBorder(new TitledBorder("在線用戶"));  
        westPanel.add(leftpanel, "North");
        westPanel.add(leftPanel, "Center");
  
        rightPanel = new JScrollPane(contentArea);  
        rightPanel.setBorder(new TitledBorder("訊息區"));  
  
        centerSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, westPanel,  
                rightPanel);  
        centerSplit.setDividerLocation(150);  
        northPanel = new JPanel(new GridLayout(1, 6));  
        northPanel.add(new JLabel("                        人數上限"));  
        northPanel.add(txt_max);  
        northPanel.add(new JLabel("                                  port"));  
        northPanel.add(txt_port);  
        northPanel.add(btn_start);  
        northPanel.add(btn_stop);  
        northPanel.setBorder(new TitledBorder("配置設定"));  
  
        frame.setLayout(new BorderLayout());  
        frame.add(northPanel, "North");  
        frame.add(centerSplit, "Center");  
        frame.add(southPanel, "South");  
        frame.setSize(800, 600);  
        //frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());//设置全屏  
        int screen_width = Toolkit.getDefaultToolkit().getScreenSize().width;  
        int screen_height = Toolkit.getDefaultToolkit().getScreenSize().height;  
        frame.setLocation((screen_width - frame.getWidth()) / 2,  (screen_height - frame.getHeight()) / 2);  
        frame.setVisible(true);  
  
        // 关闭窗口时事件  
        frame.addWindowListener(new WindowAdapter() {  
            public void windowClosing(WindowEvent e) {  
                if (isStart) {  
                    closeServer();// 关闭服务器  
                }  
                System.exit(0);// 退出程序  
            }  
        });  
  
        // 文本框按回车键时事件  
        txt_message.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                send();  
            }  
        });  
  
        // 单击发送按钮时事件  
        btn_send.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent arg0) {  
                send();  
            }  
        });  
  
        // 单击启动服务器按钮时事件  
        btn_start.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                if (isStart) {  
                    JOptionPane.showMessageDialog(frame, "伺服器早已啟動！",  
                            "Error", JOptionPane.ERROR_MESSAGE);  
                    return;  
                }  
                int max;  
                int port;  
                try {  
                    try {  
                        max = Integer.parseInt(txt_max.getText());  
                    } catch (Exception e1) {  
                        throw new Exception("人數上限請輸入正整數！");  
                    }  
                    if (max <= 0) {  
                        throw new Exception("人數上限請輸入正整數！");  
                    }  
                    try {  
                        port = Integer.parseInt(txt_port.getText());  
                    } catch (Exception e1) {  
                        throw new Exception("port請輸入正整數！");  
                    }  
                    if (port <= 0) {  
                        throw new Exception("port請輸入正整數！");  
                    }  
                    serverStart(max, port);  
                    contentArea.append("伺服器成功啟動!  人數上限:" + max + " ,port:" + port  
                            + "\r\n");  
                    JOptionPane.showMessageDialog(null, "伺服器成功啟動!", "恭喜",  
                    JOptionPane.PLAIN_MESSAGE);
                    btn_start.setEnabled(false);  
                    txt_max.setEnabled(false);  
                    txt_port.setEnabled(false);  
                    btn_stop.setEnabled(true);  
                } catch (Exception exc) {  
                    JOptionPane.showMessageDialog(null, exc.getMessage(),  
                            "Error", JOptionPane.ERROR_MESSAGE);  
                }  
            }  
        });  
  
        // 单击停止服务器按钮时事件  
        btn_stop.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                if (!isStart) {  
                    JOptionPane.showMessageDialog(null, "伺服器尚未啟動！", "Error",  
                            JOptionPane.ERROR_MESSAGE);  
                    return;  
                }  
                try {  
                    closeServer();  
                    btn_start.setEnabled(true);  
                    txt_max.setEnabled(true);  
                    txt_port.setEnabled(true);  
                    btn_stop.setEnabled(false);  
                    contentArea.append("伺服器關閉!\r\n");  
                    JOptionPane.showMessageDialog(null, "伺服器關閉!", "通知",  
                    JOptionPane.PLAIN_MESSAGE);
                } catch (Exception exc) {  
                    JOptionPane.showMessageDialog(null, "無法終止伺服器！", "Error",  
                            JOptionPane.ERROR_MESSAGE);  
                }  
            }  
        });  
    }