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

📄 chatserverframe.java

📁 Java实例入门
💻 JAVA
字号:
package simplechatserver;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;/** * Title:        网络聊天程序的服务器端 * Description: * Copyright:    Copyright (c) 2001 * Company: * @author * @version 1.0 */public class ChatServerFrame extends JFrame {  JPanel contentPane;  JLabel statusBar = new JLabel();  BorderLayout borderLayout1 = new BorderLayout();  JTextField txtSerPort = new JTextField();  JButton cmdCommand = new JButton();  JLabel jLabel1 = new JLabel();  JPanel jPanelHost = new JPanel();  JTextArea txtMsg = new JTextArea();  JTextArea txtInput = new JTextArea();  JPanel jPanelInput = new JPanel();  JScrollPane jScrollPaneInput = new JScrollPane();  JPanel jPanelMain = new JPanel();  JScrollPane jScrollPaneMsg = new JScrollPane();  JButton cmdSend = new JButton();  JPanel jPanelSend = new JPanel();  GridLayout gridLayout1 = new GridLayout();  JLabel jLabel4 = new JLabel();  JLabel jLabel3 = new JLabel();  BorderLayout borderLayout2 = new BorderLayout();  TitledBorder titledBorder1;  Border border1;  Border border2;  TitledBorder titledBorder2;  JComboBox cboClient = new JComboBox();  servThread currencyServer;  /**Construct the frame*/  public ChatServerFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  /**Component initialization*/  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(ChatServerFrame.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"需要发送的内容");    border1 = BorderFactory.createEmptyBorder();    border2 = BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134));    titledBorder2 = new TitledBorder(border2,"日志");    contentPane.setLayout(borderLayout1);    this.setSize(new Dimension(401, 435));    this.setTitle("聊天程序服务器端");    statusBar.setText(" ");    txtSerPort.setColumns(5);    cmdCommand.setText("连接");    cmdCommand.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        cmdCommand_actionPerformed(e);      }    });    jLabel1.setText("请输入服务器地址:");    jPanelInput.setLayout(borderLayout2);    jPanelMain.setLayout(gridLayout1);    cmdSend.setText("发送");    cmdSend.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        cmdSend_actionPerformed(e);      }    });    gridLayout1.setRows(2);    gridLayout1.setColumns(1);    gridLayout1.setHgap(1);    jLabel4.setText(" ");    jLabel3.setText(" ");    jLabel1.setText("请输入服务端口号:");    txtSerPort.setText("1200");    txtSerPort.setColumns(6);    cmdCommand.setText("Start");    gridLayout1.setRows(2);    gridLayout1.setColumns(1);    jScrollPaneInput.setBorder(titledBorder1);    jScrollPaneMsg.setBorder(titledBorder2);    cmdSend.setText("发送至");    contentPane.add(statusBar, BorderLayout.SOUTH);    contentPane.add(jPanelHost,  BorderLayout.NORTH);    jPanelHost.add(jLabel1, null);    jPanelHost.add(txtSerPort, null);    jPanelHost.add(cmdCommand, null);    contentPane.add(jPanelMain, BorderLayout.CENTER);    jPanelInput.add(jScrollPaneInput, BorderLayout.CENTER);    jPanelInput.add(jLabel3, BorderLayout.WEST);    jPanelInput.add(jLabel4, BorderLayout.EAST);    jPanelInput.add(jPanelSend, BorderLayout.SOUTH);    jPanelSend.add(cmdSend, null);    jScrollPaneMsg.getViewport().add(txtMsg, null);    jScrollPaneInput.getViewport().add(txtInput, null);    jPanelMain.add(jPanelInput, null);    jPanelMain.add(jScrollPaneMsg, null);    jPanelSend.add(cboClient, null);  }  /**Overridden so we can exit when window is closed*/  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  void cmdCommand_actionPerformed(ActionEvent e) {    if(cmdCommand.getText().equals("Start")){       if(currencyServer!=null) {          currencyServer.stopServer() ;          currencyServer=null;       }       int port=Integer.parseInt(txtSerPort.getText().trim() );       currencyServer=new servThread(port);       currencyServer.start();       currencyServer.setClientList(cboClient);       currencyServer.setOutTextComponent(txtMsg);       statusBar.setText("服务启动,端口为"+port);       txtMsg.setText(txtMsg.getText()+"服务启动,端口为"+port+"\n");       cmdCommand.setText("Stop");    }    else{      if(currencyServer!=null) {          currencyServer.stopServer() ;          currencyServer=null;       }       statusBar.setText("服务停止");       txtMsg.setText(txtMsg.getText()+"服务停止\n");       cmdCommand.setText("Start");    }  }  void cmdSend_actionPerformed(ActionEvent e) {    if(currencyServer!=null){        String strSend=txtInput.getText().trim();        String strClient=cboClient.getSelectedItem().toString();        synchronized(this){           String result=(currencyServer.send(strSend,strClient)?"成功":"失败");           txtMsg.setText(txtMsg.getText()+"向"+strClient+"发送 :"+txtInput.getText().trim()+"\n"+result+"\n");       }     }  }}

⌨️ 快捷键说明

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