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

📄 chatclientframe.java

📁 Java实例入门
💻 JAVA
字号:
package simplechatclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import java.net.*;/** * Title:        聊天小程序 * Description:  这是一个使用SOCKET制作的网络聊天程序 * Copyright:    Copyright (c) 2001 * Company: * @author * @version 1.0 */public class ChatClientFrame extends JFrame {  JPanel contentPane;  JLabel statusBar = new JLabel();  BorderLayout borderLayout1 = new BorderLayout();  JPanel jPanelHost = new JPanel();  JLabel jLabel1 = new JLabel();  JTextField txtHostAdd = new JTextField();  JLabel jLabel2 = new JLabel();  JTextField txtSerPort = new JTextField();  JButton cmdCon = new JButton();  JPanel jPanelMain = new JPanel();  GridLayout gridLayout1 = new GridLayout();  JPanel jPanelInput = new JPanel();  BorderLayout borderLayout2 = new BorderLayout();  JScrollPane jScrollPaneInput = new JScrollPane();  TitledBorder titledBorder1;  JLabel jLabel3 = new JLabel();  JLabel jLabel4 = new JLabel();  JTextArea txtInput = new JTextArea();  JPanel jPanelSend = new JPanel();  JButton cmdSend = new JButton();  JTextArea txtMsg = new JTextArea();  JScrollPane jScrollPaneMsg = new JScrollPane();  ChatSocket currencyChat;  Border border1;  TitledBorder titledBorder2;  /**Construct the frame*/  public ChatClientFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  /**Component initialization*/  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(ChatFrame.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    titledBorder1 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"需要发送的信息");    border1 = BorderFactory.createEmptyBorder();    titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(134, 134, 134)),"日志");    contentPane.setLayout(borderLayout1);    this.setSize(new Dimension(421, 399));    this.setTitle("网络聊天客户端程序");    statusBar.setText(" ");    jLabel1.setText("请输入服务器地址:");    txtHostAdd.setColumns(10);    jLabel2.setToolTipText("");    jLabel2.setText("端口:");    txtSerPort.setColumns(5);    cmdCon.setText("Connect");    cmdCon.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        cmdCon_actionPerformed(e);      }    });    jPanelMain.setLayout(gridLayout1);    gridLayout1.setRows(2);    gridLayout1.setColumns(1);    gridLayout1.setHgap(1);    jPanelInput.setLayout(borderLayout2);    jScrollPaneInput.setBorder(titledBorder1);    jLabel3.setText(" ");    jLabel4.setText(" ");    cmdSend.setText("发送");    cmdSend.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        cmdSend_actionPerformed(e);      }    });    jScrollPaneMsg.setBorder(titledBorder2);    txtMsg.setEditable(false);    contentPane.add(statusBar, BorderLayout.SOUTH);    contentPane.add(jPanelHost, BorderLayout.NORTH);    jPanelHost.add(jLabel1, null);    jPanelHost.add(txtHostAdd, null);    jPanelHost.add(jLabel2, null);    jPanelHost.add(txtSerPort, null);    jPanelHost.add(cmdCon, null);    contentPane.add(jPanelMain, BorderLayout.CENTER);    jPanelMain.add(jPanelInput, null);    jPanelInput.add(jScrollPaneInput, BorderLayout.CENTER);    jPanelInput.add(jLabel3,  BorderLayout.WEST);    jPanelInput.add(jLabel4,  BorderLayout.EAST);    jPanelInput.add(jPanelSend,  BorderLayout.SOUTH);    jScrollPaneInput.getViewport().add(txtInput, null);    jPanelSend.add(cmdSend, null);    jPanelMain.add(jScrollPaneMsg, null);    jScrollPaneMsg.getViewport().add(txtMsg,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 cmdCon_actionPerformed(ActionEvent e) {    if(cmdCon.getText().equals("Connect")){       if(currencyChat!=null)  {           currencyChat.disConnect();           currencyChat=null;        }       int port=Integer.parseInt(txtSerPort.getText().trim());        currencyChat=new ChatSocket(txtHostAdd.getText().trim(),port);        currencyChat.setOutTextComponent(txtMsg);        if(currencyChat.connect()){           statusBar.setText("连接服务器"+txtHostAdd.getText().trim()+":"+port+"成功!") ;         }        else{           statusBar.setText("连接服务器"+txtHostAdd.getText().trim()+":"+port+"成功!") ;         }         txtMsg.setText(txtMsg.getText()+statusBar.getText()+"\n");         cmdCon.setText("Disconnect");    }else{       if(currencyChat!=null)  {           currencyChat.disConnect();           currencyChat=null;        }        statusBar.setText("关闭连接") ;        txtMsg.setText(txtMsg.getText()+"关闭连接\n");        cmdCon.setText("Connect");    }  }  void cmdSend_actionPerformed(ActionEvent e) {    if(currencyChat!=null)  {       synchronized(this){           String result=(currencyChat.send(txtInput.getText().trim())?"成功":"失败");           txtMsg.setText(txtMsg.getText()+"发送 :"+txtInput.getText().trim()+"\n"+result+"\n");       }    }  }}

⌨️ 快捷键说明

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