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

📄 frame1.java

📁 类似于QQ的简易聊天程序
💻 JAVA
字号:
package qqclient;

import java.awt.BorderLayout;
import java.net.*;
import java.io.*;
import javax.swing.JFrame;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JList;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.JLabel;

public class Frame1 extends JFrame implements WindowListener {
    XYLayout xYLayout1 = new XYLayout();
    JButton jButton1 = new JButton();
    JComboBox jComboBox1 = new JComboBox();
    TextField TextField1 = new TextField();
    List List1 = new List();
    Socket s=null;
    JButton jButton2 = new JButton();
    JTextField jTextField1 = new JTextField();
    JLabel jLabel1 = new JLabel();
    public Frame1() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(xYLayout1);
        this.addWindowListener(this);
        this.setSize(400,400);
        jButton1.setText("发送");
        jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
        jComboBox1.addActionListener(new Frame1_jComboBox1_actionAdapter(this));
        xYLayout1.setWidth(400);
        xYLayout1.setHeight(351);
        jButton2.setText("连接服务器");
        jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
        jLabel1.setText("服务器名称");
        this.getContentPane().add(jButton1, new XYConstraints(308, 261, 75, 23));
        this.getContentPane().add(jComboBox1, new XYConstraints(12, 260, 95, 24));
        this.getContentPane().add(TextField1,
        new XYConstraints(113, 259, 191, 27));
        this.getContentPane().add(List1, new XYConstraints(13, 23, 367, 227));
        this.getContentPane().add(jLabel1, new XYConstraints(33, 302, 96, 39));
        this.getContentPane().add(jButton2, new XYConstraints(260, 303, 102, 28));
        this.getContentPane().add(jTextField1,
        new XYConstraints(125, 305, 108, 28));
        this.setVisible(true);
        this.jComboBox1.addItem("所有人");

    }

    public static void main(String[] args) {
        Client.f=new Frame1();
    }

    /**
     * Invoked the first time a window is made visible.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowOpened(WindowEvent e) {
    }

    /**
     * Invoked when the user attempts to close the window from the window's
     * system menu.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }

    /**
     * Invoked when a window has been closed as the result of calling dispose
     * on the window.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowClosed(WindowEvent e) {
    }

    /**
     * Invoked when a window is changed from a normal to a minimized state.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowIconified(WindowEvent e) {
    }

    /**
     * Invoked when a window is changed from a minimized to a normal state.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowDeiconified(WindowEvent e) {
    }

    /**
     * Invoked when the Window is set to be the active Window.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowActivated(WindowEvent e) {
    }

    /**
     * Invoked when a Window is no longer the active Window.
     *
     * @param e WindowEvent
     * @todo Implement this java.awt.event.WindowListener method
     */
    public void windowDeactivated(WindowEvent e) {
    }

    public void jButton1_actionPerformed(ActionEvent e) {


        try {
            PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
            String shuo=TextField1.getText();
            String ren=(String)jComboBox1.getSelectedItem();
            System.out.println("ren"+ren);
            if(ren.equals("所有人")){
                pw.println("CAll:"+Client.f.getTitle()+"-"+ren+"="+shuo);

                this.List1.add("你对"+ren+"说"+shuo);
                System.out.println("CAll:"+Client.f.getTitle()+"-"+ren+"="+shuo);
        }
            else{
                pw.println("COne:"+Client.f.getTitle()+"-"+ren+"="+shuo);
                this.List1.add("你对"+ren+"说:"+shuo);
                System.out.println("Cone"+"__________________________________");
            }
        } catch (IOException ex) {
          }
      }

    public void jComboBox1_actionPerformed(ActionEvent e) {

    }

    public void jButton2_actionPerformed(ActionEvent e) {
        if(e.getSource()==jButton2){
        if(!jTextField1.getText().equals("")){
            try {
                String fuwuqi=jTextField1.getText();
                s = new Socket(fuwuqi,8080);
                new CInputStream(s.getInputStream()).start();
                PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
                pw.println("CLogin:");
                this.jButton2.setEnabled(false);
                this.jTextField1.setText("");
            } catch (IOException ex2) {
            }
        }
        else{

            javax.swing.JOptionPane.showMessageDialog(null,"请输入要连接的服务器名称");
        }

    }

    }
}


class Frame1_jButton2_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jButton2_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}


class Frame1_jComboBox1_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jComboBox1_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jComboBox1_actionPerformed(e);
    }
}


class Frame1_jButton1_actionAdapter implements ActionListener {
    private Frame1 adaptee;
    Frame1_jButton1_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
class CInputStream extends Thread{
    InputStream in;
    public CInputStream(InputStream in){
        this.in=in;
    }
    public void run(){
        BufferedReader buff=new BufferedReader(new InputStreamReader(in));
        while(true){
            String s = null;
            try {
                s = buff.readLine();
                System.out.println(s);
            } catch (IOException ex) {
            }
            String sd=s.substring(s.indexOf("=")+1);
            System.out.println(sd+"aaaaaaaaaaaaaaaaa");
            String ss=sd.substring(0,sd.indexOf(":"));
            System.out.println(ss);
            if(ss.equals("SOneLogin")){
                String sss=s.substring(0,s.indexOf("="));
                System.out.println(sss+"=============");
                Client.f.setTitle(sss);
                System.out.println(sss);
                Client.f.List1.add(sss+"登陆成功");
            }
            if(ss.equals("SAllLogin")){
                String zaixian=sd.substring(sd.indexOf(":")+1);
                System.out.println(zaixian+"-------------");
                String title=Client.f.getTitle();
                System.out.println("title"+title);
              if(!zaixian.equals(title)){
                    Client.f.jComboBox1.addItem(zaixian);
                    Client.f.List1.add(zaixian+"已登陆");
                }
            }
            if(ss.equals("ZaiXian")){
                String xian=sd.substring(sd.indexOf(":")+1);
                String tmp[]=xian.split(";");
                for(int i=0;i<tmp.length;i++){
                    if(!tmp[i].equals(Client.f.getTitle())){
                        Client.f.jComboBox1.addItem(tmp[i]);
                        Client.f.List1.add(tmp[i]+"已登陆");
                        }
                }


            }

            if(ss.equals("SOne")){
                String xiaoxi=s.substring(s.indexOf(":")+1);
                System.out.println("Sone"+xiaoxi);
                String s0=xiaoxi.substring(0,xiaoxi.indexOf(">"));
                String str=s.substring(s.indexOf(">")+1);
                Client.f.List1.add(s0+"对您说"+":"+str);
            }
            if(ss.equals("SAll")){
                String xiaoxi=s.substring(s.indexOf(":")+1);
                String str=s.substring(s.indexOf(">")+1);
                String s1=xiaoxi.substring(0,xiaoxi.indexOf(">"));
                Client.f.List1.add(s1+"对所有人说"+"===="+str);

            }
        }
    }

}
class Client{
    public static Frame1 f;
}

⌨️ 快捷键说明

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