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

📄 chatclientframe.java~493~

📁 此软件为简单的实时通信软件,带文件传输与语音功能,类似于QQ
💻 JAVA~493~
📖 第 1 页 / 共 3 页
字号:
package chatclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Thread;
import javax.sound.sampled.*;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class chatClientFrame extends JFrame {
  JPanel contentPane;
  JPanel jPanel1 = new JPanel();
  JPanel jPanel2 = new JPanel();
  PaneLayout paneLayout1 = new PaneLayout();
  XYLayout xYLayout1 = new XYLayout();
  JPanel jPanel4 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField();
  JLabel jLabel2 = new JLabel();
  JTextField jTextField2 = new JTextField();
  JLabel jLabel3 = new JLabel();
  JTextField jTextField3 = new JTextField();
  JButton jButton1 = new JButton();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel5 = new JPanel();
  JPanel jPanel6 = new JPanel();
  PaneLayout paneLayout2 = new PaneLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JList jList1 = new JList();
  JLabel jLabel4 = new JLabel();
  PaneLayout paneLayout3 = new PaneLayout();
  JPanel jPanel7 = new JPanel();
  JPanel jPanel8 = new JPanel();
  JPanel jPanel9 = new JPanel();
  PaneLayout paneLayout4 = new PaneLayout();
  JPanel jPanel3 = new JPanel();
  JPanel jPanel10 = new JPanel();
  JPanel jPanel11 = new JPanel();
  PaneLayout paneLayout5 = new PaneLayout();
  JScrollPane jScrollPane2 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();
  JRadioButton jRadioButton1 = new JRadioButton();
  XYLayout xYLayout2 = new XYLayout();
  JRadioButton jRadioButton2 = new JRadioButton();
  XYLayout xYLayout3 = new XYLayout();
  JPanel jPanel12 = new JPanel();
  PaneLayout paneLayout7 = new PaneLayout();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  XYLayout xYLayout4 = new XYLayout();
  JButton jButton5 = new JButton();
  XYLayout xYLayout5 = new XYLayout();
  ButtonGroup buttonGroup1 = new ButtonGroup();
  JTextField jTextField4 = new JTextField();
  JButton jButton6 = new JButton();
  JButton jButton7 = new JButton();
  JButton jButton8 = new JButton();
  JPanel jPanel13 = new JPanel();
  PaneLayout paneLayout6 = new PaneLayout();
  PaneLayout paneLayout8 = new PaneLayout();
  JLabel jLabel5 = new JLabel();
  JButton jButton9 = new JButton();
  JButton jButton10 = new JButton();
  JButton jButton11 = new JButton();
  JLabel jLabel6 = new JLabel();
  Socket socket=null;
  PrintStream ps=null;
  Listen listen=null;
  Playback player=null;
  Capture cap=null;
  filesend filesend=null;
  filesave filesave=null;
  int count=0;
  String ip;
  String souip;
  String souname;
  String filesname;
  String filespath;
  String filesip;
  String filefname;
  String filename;
  long filelen;
  DefaultListModel   dld   =   new   DefaultListModel() ;

  //Construct the frame
  public chatClientFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(paneLayout1);
    this.setSize(new Dimension(453, 394));
    this.setTitle("chatClient");
    this.addWindowListener(new chatClientFrame_this_windowAdapter(this));
    jPanel1.setLayout(xYLayout1);
    jPanel4.setLayout(borderLayout1);
    jLabel1.setText("服务器IP");
    jPanel1.setBackground(UIManager.getColor("control"));
    jTextField1.setText("192.168.1.2");
    jLabel2.setText("服务器端口");
    jTextField2.setText("4000");
    jLabel3.setText("昵称");
    jTextField3.setText("SAM");
    jButton1.setText("连接");
    jButton1.addActionListener(new chatClientFrame_jButton1_actionAdapter(this));
    jPanel6.setLayout(paneLayout2);
    jLabel4.setText("目前用户数:0");
    jPanel5.setLayout(paneLayout3);
    jPanel2.setLayout(paneLayout4);
    jPanel7.setLayout(paneLayout5);
    jPanel3.setLayout(paneLayout6);
    jRadioButton1.setText("广播发送");
    jPanel10.setLayout(xYLayout2);
    jRadioButton2.setSelected(true);
    jRadioButton2.setText("选择发送");
    jPanel11.setLayout(xYLayout3);
    jPanel12.setLayout(paneLayout7);
    jButton2.setText("发送");
    jButton2.addActionListener(new chatClientFrame_jButton2_actionAdapter(this));
    jButton3.setText("退出");
    jButton3.addActionListener(new chatClientFrame_jButton3_actionAdapter(this));
    jButton4.setText("发送语音请求");
    jButton4.addActionListener(new chatClientFrame_jButton4_actionAdapter(this));
    jPanel8.setLayout(xYLayout4);
    jButton5.setText("发送文件");
    jButton5.addActionListener(new chatClientFrame_jButton5_actionAdapter(this));
    jPanel9.setLayout(xYLayout5);
    jTextArea1.setEditable(false);
    jTextArea1.setText("");
    jButton6.setEnabled(false);
    jButton6.setContentAreaFilled(true);
    jButton6.setText("接受");
    jButton6.addActionListener(new chatClientFrame_jButton6_actionAdapter(this));
    jButton7.setEnabled(false);
    jButton7.setText("拒绝");
    jButton7.addActionListener(new chatClientFrame_jButton7_actionAdapter(this));
    jButton8.setEnabled(false);
    jButton8.setContentAreaFilled(true);
    jButton8.setFocusPainted(true);
    jButton8.setText("结束");
    jButton8.addActionListener(new chatClientFrame_jButton8_actionAdapter(this));
    jList1.setEnabled(true);
    jList1.setDoubleBuffered(false);
    jList1.setToolTipText("");
    jList1.setFixedCellHeight(-1);
    jList1.setVisibleRowCount(8);
    jPanel3.setPreferredSize(new Dimension(10, 50));
    jPanel13.setLayout(paneLayout8);
    jLabel5.setText("未连接");
    jButton9.setEnabled(false);
    jButton9.setText("接受");
    jButton9.addActionListener(new chatClientFrame_jButton9_actionAdapter(this));
    jButton10.setEnabled(false);
    jButton10.setText("拒绝");
    jButton10.addActionListener(new chatClientFrame_jButton10_actionAdapter(this));
    jButton11.setEnabled(false);
    jButton11.setText("停止");
    jButton11.addActionListener(new chatClientFrame_jButton11_actionAdapter(this));
    jLabel6.setText("文件状态:没有文件传输");
    jScrollPane2.setToolTipText("");
    jPanel10.add(jRadioButton1, new XYConstraints(10, 0, -1, -1));
    jPanel10.add(jRadioButton2, new XYConstraints(99, 1, -1, -1));
    jPanel1.add(jPanel4,          new XYConstraints(-1, 0, 101, 203));
    jPanel4.add(jPanel5, BorderLayout.SOUTH);
    jPanel5.add(jLabel4,  new PaneConstraints("jLabel4", "jLabel4", PaneConstraints.ROOT, 1.0f));
    jPanel4.add(jPanel6, BorderLayout.CENTER);
    jPanel6.add(jScrollPane1, new PaneConstraints("jScrollPane1", "jScrollPane1", PaneConstraints.ROOT, 1.0f));
    jScrollPane1.getViewport().add(jList1, null);
    jPanel1.add(jLabel1,   new XYConstraints(4, 206, -1, -1));
    jPanel1.add(jTextField1,      new XYConstraints(2, 222, 96, -1));
    jPanel1.add(jLabel2,   new XYConstraints(4, 248, -1, 14));
    jPanel1.add(jTextField2,   new XYConstraints(2, 263, 94, -1));
    jPanel1.add(jLabel3,  new XYConstraints(4, 289, -1, -1));
    jPanel1.add(jTextField3,   new XYConstraints(3, 304, 93, -1));
    jPanel1.add(jButton1,    new XYConstraints(9, 333, 71, -1));
    jPanel2.add(jPanel7,                  new PaneConstraints("jPanel7", "jPanel7", PaneConstraints.ROOT, 0.5f));
    jPanel2.add(jPanel8,                  new PaneConstraints("jPanel8", "jPanel7", PaneConstraints.BOTTOM, 0.33756346f));
    jPanel8.add(jButton4, new XYConstraints(3, 5, 105, -1));
    jPanel8.add(jButton6,       new XYConstraints(117, 5, -1, -1));
    jPanel8.add(jButton7,  new XYConstraints(186, 5, -1, -1));
    jPanel8.add(jButton8,  new XYConstraints(254, 6, -1, -1));
    jPanel2.add(jPanel9,                  new PaneConstraints("jPanel9", "jPanel8", PaneConstraints.BOTTOM, 0.7067669f));
    jPanel9.add(jButton5,    new XYConstraints(10, 4, -1, -1));
    jPanel9.add(jButton9,   new XYConstraints(110, 5, -1, -1));
    jPanel9.add(jButton10,   new XYConstraints(186, 5, -1, -1));
    jPanel9.add(jButton11,  new XYConstraints(257, 5, -1, -1));
    jPanel9.add(jLabel6,      new XYConstraints(13, 47, 305, 25));
    contentPane.add(jPanel1, new PaneConstraints("jPanel1", "jPanel1", PaneConstraints.ROOT, 0.5f));
    contentPane.add(jPanel2, new PaneConstraints("jPanel2", "jPanel1", PaneConstraints.RIGHT, 0.7635934f));
    buttonGroup1.add(jRadioButton1);
    buttonGroup1.add(jRadioButton2);
    jPanel11.add(jPanel12,  new XYConstraints(1, 9, 330, -1));
    jPanel12.add(jTextField4, new PaneConstraints("jTextField4", "jTextField4", PaneConstraints.ROOT, 1.0f));
    jPanel11.add(jButton3, new XYConstraints(145, 44, 77, -1));
    jPanel11.add(jButton2, new XYConstraints(231, 44, 70, -1));
    jPanel7.add(jPanel3, new PaneConstraints("jPanel3", "jPanel3", PaneConstraints.ROOT, 0.5f));
    jScrollPane2.getViewport().add(jTextArea1, null);
    jPanel7.add(jPanel10, new PaneConstraints("jPanel10", "jPanel3", PaneConstraints.BOTTOM, 0.4202335f));
    jPanel7.add(jPanel11, new PaneConstraints("jPanel11", "jPanel10", PaneConstraints.BOTTOM, 0.8055556f));
    jPanel3.add(jScrollPane2,   new PaneConstraints("jScrollPane2", "jScrollPane2", PaneConstraints.ROOT, 0.5f));
    jPanel3.add(jPanel13,   new PaneConstraints("jPanel13", "jScrollPane2", PaneConstraints.TOP, 0.09271526f));
    jPanel13.add(jLabel5,  new PaneConstraints("jLabel5", "jLabel5", PaneConstraints.ROOT, 1.0f));
  }
  //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 jButton1_actionPerformed(ActionEvent e) {
    if(socket==null){
               try{
                       socket=new Socket(jTextField1.getText(),Integer.parseInt(jTextField2.getText()));
                       if(socket!=null){
                               ps=new PrintStream(socket.getOutputStream());
                               StringBuffer info=new StringBuffer("PEOPLE:");
                               ip=InetAddress.getLocalHost().getHostAddress();
                               String userinfo=jTextField3.getText()+":"+ip;
                               ps.println(info.append(userinfo));
                               ps.flush();
                               listen=new Listen(this,jTextField3.getText(),socket);
                               listen.start();
                       }
                       else{
                               jTextArea1.append(jTextField3.getText()+"登陆失败\n");
                       }
               }
               catch(IOException ee){
                       jTextArea1.append(jTextField3.getText()+"登陆失败\n");
                       disconnect();
               }
       }
  }

  void jButton2_actionPerformed(ActionEvent e) {
    if(socket!=null){
                StringBuffer message=new StringBuffer("MSG:");
              if(jRadioButton1.isSelected()){
                        message.append("BROAD:");
                        jTextArea1.append(listen.name+"->"+"ALL"+"\n:"+jTextField4.getText()+"\n");

                }
              else if(jRadioButton2.isSelected()){
                   if(jList1.getSelectedValue()!=null){
                        message.append((String)jList1.getSelectedValue()+":");
                        jTextArea1.append(listen.name+"->"+(String)jList1.getSelectedValue()+"\n:"+jTextField4.getText()+"\n");
                        }
                  else
                    JOptionPane.showMessageDialog(null, "请选择用户!", "错误!",JOptionPane.INFORMATION_MESSAGE);
                 }
                message.append(listen.name+":");
                ps.println(message.append(jTextField4.getText()));
                jTextField4.setText("");
                ps.flush();
        }
  }
  class Listen extends Thread{
    String name = null;
    DataInputStream dis = null;
    PrintStream ps = null;
    Socket socket = null;
    chatClientFrame parent = null;

    public Listen(chatClientFrame p, String n, Socket s) {
      parent = p;
      name = n;
      socket = s;
      try {
        dis = new DataInputStream(s.getInputStream());
        ps = new PrintStream(s.getOutputStream());
      }
      catch (IOException e) {
        parent.jTextArea1.append("Error:  " + e + "\n");
        parent.disconnect();
      }
    }

    public void run() {
      String message = null;
      while (true) {
        try {
          message = dis.readLine();
        }
        catch (IOException e) {
          parent.jTextArea1.append("Error: " + e + "\n");
          parent.disconnect();
        }
        if (message == null) {
          parent.listen = null;
          parent.socket = null;
          ((DefaultListModel)parent.jList1.getModel()).removeAllElements();
          return;
        }
        StringTokenizer str = new StringTokenizer(message, ":");
        String keyword = str.nextToken();
        if (keyword.equalsIgnoreCase("PEOPLE")) {
          count = 0;
          ((DefaultListModel)parent.jList1.getModel()).removeAllElements();
          while (
              str.hasMoreTokens()) {
            String st = str.nextToken();
            parent.jList1.setModel(dld);
            dld.addElement(st);
            count++;
          }
          jLabel4.setText("目前用户数: " + String.valueOf(count));
        }
        else if (keyword.equalsIgnoreCase("MSG")) {
          String broadcast = str.nextToken();
          String usr = str.nextToken();
          parent.jTextArea1.append(usr + "\n");
          parent.jTextArea1.append(str.nextToken("\0"));
          parent.jTextArea1.append("\n");
        }
        else if (keyword.equalsIgnoreCase("QUIT")) {
          String who = str.nextToken();
          if (who.equalsIgnoreCase("SERVER")) {
            parent.jTextArea1.append("服务器关闭\n");
            ((DefaultListModel)parent.jList1.getModel()).removeAllElements();
            jLabel4.setText("目前用户数:0");
            jLabel5.setText("未连接");
            try {
              parent.listen.stop();
              parent.listen = null;
              parent.socket.close();
              parent.socket = null;
            }
            catch (IOException e) {
              parent.jTextArea1.append("Error: " + e + "\n");
            }
            ((DefaultListModel) parent.jList1.getModel()).removeAllElements();
            return;
          }
          else if (who.equalsIgnoreCase("DISC")) {
            parent.jTextArea1.append("服务器要求断开连接!\n");
            ((DefaultListModel)parent.jList1.getModel()).removeAllElements();
            jLabel4.setText("目前用户数:0");
            jLabel5.setText("未连接");
            disconnect();
          try {
           parent.listen.stop();
           parent.listen = null;
           parent.socket.close();
           parent.socket = null;
         }
         catch (IOException e) {
           parent.jTextArea1.append("Error: " + e + "\n");
         }
         return;
          }
          else {
            parent.jTextArea1.append(who + "已经离开\n");
          }

⌨️ 快捷键说明

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