⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 自己写的一个聊天的小程序 请多多指教
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        beginListening.addActionListener(this);        disconnect.addActionListener(this);        disconnectAll.addActionListener(this);        getContentPane().setLayout(null);        addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosing(java.awt.event.WindowEvent evt) {                exitForm(evt);            }        });        panelLeft.setLayout(null);        panelLeft.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));        clientList.setMaximumSize(new java.awt.Dimension(170, 260));        clientList.setMinimumSize(new java.awt.Dimension(120, 100));        panelLeft.add(clientList);        clientList.setBounds(10, 30, 150, 240);        clientListLabel.setText("Online Clients");        panelLeft.add(clientListLabel);        clientListLabel.setBounds(10, 10, 130, 17);        disconnect.setText("Disconnect");        panelLeft.add(disconnect);        disconnect.setBounds(10, 280, 150, 27);        disconnectAll.setText("Disconnect All");        panelLeft.add(disconnectAll);        disconnectAll.setBounds(10, 310, 150, 27);        getContentPane().add(panelLeft);        panelLeft.setBounds(20, 20, 170, 350);        panelRightTop.setLayout(null);        panelRightTop.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));        clientMessagesLabel.setText("Messages");        panelRightTop.add(clientMessagesLabel);        clientMessagesLabel.setBounds(10, 10, 120, 17);        messagesScroller.setViewportView(clientMessages);        panelRightTop.add(messagesScroller);        messagesScroller.setBounds(10, 30, 150, 190);        getContentPane().add(panelRightTop);        panelRightTop.setBounds(210, 20, 170, 230);        panelRightBottom.setLayout(null);        panelRightBottom.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));        portLabel.setText("Port");        panelRightBottom.add(portLabel);        portLabel.setBounds(10, 10, 100, 17);        panelRightBottom.add(port);        port.setBounds(10, 30, 140, 21);        port.setText(Integer.toString(PORT_NUMBER));        beginListening.setText("Begin Listening");        panelRightBottom.add(beginListening);        beginListening.setBounds(10, 60, 140, 27);        getContentPane().add(panelRightBottom);        panelRightBottom.setBounds(210, 260, 170, 110);        setSize(420,420);    }    /**     *   This method is implements is called, the window of the server     *   application is closed. It terminates the server application.     *   @pre true     *   @post true     */    private void exitForm(java.awt.event.WindowEvent evt) {        serverConnectionManager.dispose();        System.exit(0);    }    /**     *   This method is called if one of the buttons of the GUI is pressed.     *   This causes an action event which causes the corresponding     *   reaction of the server application.     *   @pre true     *   @post true     *   @param e ActionEvent object which encapsulates the information     *          about the event.     */    public void actionPerformed(ActionEvent e) {        // source        Object o = e.getSource();        // if the listener is started        if ((o == beginListening) && (listening==false)) {            try {                // set the port number                serverConnectionManager.setPortNumber(Integer.parseInt(port.getText()));            } catch (Exception ex) {                // if port number is not correctly entered set the old                // portnumber back into the port textfield                port.setText(Integer.toString(serverConnectionManager.getPortNumber()));            }            // start the listening process            serverConnectionManager.beginListening();            // set new text for the button            beginListening.setText("Stop Listening");            // set flag to listening            listening = true;        } else { // otherwise check            // if we are listnening an the beginlistening button            // (in this case with the caption "Stop Listening")            // is pressed...            if ((o == beginListening) && (listening==true)) {                // ...stop the listening process                serverConnectionManager.stopListening();                // old caption for the button                beginListening.setText("Start Listening");                // set flag back to listening.                listening = false;            }        }        if (o == disconnect) {            // disconnect was pressed, disconnect the chosen            // client            Object id = clientList.getSelectedValue();            if (id != null) { // check of if a client was connected, otherwise                              // precondition of the closeConnection method is                              // violated                serverConnectionManager.closeConnection(id);            }        }        // close all connections        if (o == disconnectAll) {            // disconnect all clients            serverConnectionManager.closeAllConnections();        }    }    /**     *   Starts the server application.     *   @pre true     *   @post true     *   @param args String parameters of the application - not used.     */    public static void main(String[] args) {        new Server().setVisible(true);    }    /**     *  This interface method is called by objects of the communication component.     *  This class has to register at the {@link #serverConnectionManager}     *  object by calling the method {@link net.sf.cscc.ServerConnectionManager#addObserver(DataEventBaseObserver o)}.     *     *   @pre true     *   @post true     *   @see net.sf.cscc.DataEventObserver#receiveEvent()     */    public void receiveEvent(DataEvent de) {        if (((Integer)de.getEventKey()).intValue() == 1) {            clientMessages.append("Client " + de.getClientId() + " sends data: " +  de.getData().toString() + "\n");        }    }     /**     *   This interface method is called by objects of the {@link net.sf.cscc.CommunicationEventObservable}     *   class. This is done by calling the method {@link net.sf.cscc.ServerConnectionManager#addObserver(CommunicationEventObserver o)}     *   for observing the communication events of the server.     *     *   @pre true     *   @post true     *   @param ce CommunicationEvent which is delivered to the implementor of     *             this interface (ie. the observer).     */    public void receiveEvent(CommunicationEvent ce) {        switch (ce.getEventId()) {            case (CommunicationEvent.CONNECTION_ESTABLISHED):                clientMessages.append("connection opened! of client " + ce.getData() + "\n");                clients.add(ce.getData());                clientList.setListData(clients);                break;            case (CommunicationEvent.CONNECTION_BROKEN):                clientMessages.append("connection broken! of client " + ce.getData() + "\n");                clients.remove(ce.getData());                clientList.setListData(clients);                break;            case (CommunicationEvent.CONNECTION_CLOSED):                clientMessages.append("connection closed! of client " + ce.getData() + "\n");                clients.remove(ce.getData());                clientList.setListData(clients);                break;            case (CommunicationEvent.CONNECTION_START_LISTENING):                clientMessages.append("Start listening! \n");                break;            case (CommunicationEvent.CONNECTION_STOP_LISTENING):                clientMessages.append("Stop listening! \n");                break;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -