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

📄 frmclient.java~14~

📁 使用JBuilder开发的简单聊天程序
💻 JAVA~14~
字号:
package talk;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.net.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class FrmClient extends JFrame {
    JPanel contentPane;
    JLabel lblIP = new JLabel();
    JTextField txtIP = new JTextField();
    JLabel lblPort = new JLabel();
    JTextField txtPort = new JTextField();
    JButton btnConnection = new JButton();
    JButton btnExit = new JButton();
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextArea txtMessage = new JTextArea();
    JScrollPane jScrollPane2 = new JScrollPane();
    JTextArea txtContent = new JTextArea();
    JButton btnSend = new JButton();
    //客户端套接字
    Socket s=null;
    //输入流
    DataInputStream in;
    //输出流
    DataOutputStream out;
    //接收信息
    String s1;
    //判断是否连接服务器
    static boolean IsConnectionWithServer=false;
    JButton btnEx = new JButton();
    public FrmClient() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(null);
        setSize(new Dimension(400, 424));
        setTitle("客户端");
        lblIP.setForeground(Color.blue);
        lblIP.setHorizontalAlignment(SwingConstants.CENTER);
        lblIP.setText("IP地址:");
        lblIP.setBounds(new Rectangle(22, 31, 79, 21));
        txtIP.setBounds(new Rectangle(112, 31, 113, 21));
        lblPort.setForeground(Color.blue);
        lblPort.setHorizontalAlignment(SwingConstants.CENTER);
        lblPort.setText("端口号:");
        lblPort.setBounds(new Rectangle(22, 72, 79, 21));
        txtPort.setBounds(new Rectangle(112, 72, 113, 21));
        btnConnection.setBounds(new Rectangle(273, 28, 83, 25));
        btnConnection.setForeground(Color.blue);
        btnConnection.setText("连接");
        btnConnection.addActionListener(new
                                        FrmClent_btnConnection_actionAdapter(this));
        btnExit.setBounds(new Rectangle(206, 370, 83, 25));
        btnExit.setForeground(Color.blue);
        btnExit.setText("断开");
        btnExit.addActionListener(new FrmClent_btnExit_actionAdapter(this));
        jScrollPane1.setBounds(new Rectangle(26, 109, 350, 149));
        txtMessage.setEnabled(false);
        txtMessage.setForeground(Color.blue);
        txtMessage.setLineWrap(true);
        jScrollPane2.setBounds(new Rectangle(26, 266, 349, 83));
        txtContent.setForeground(Color.blue);
        txtContent.setLineWrap(true);
        btnSend.setBounds(new Rectangle(94, 370, 83, 25));
        btnSend.setForeground(Color.blue);
        btnSend.setText("发送");
        btnSend.addActionListener(new FrmClent_btnSend_actionAdapter(this));
        btnEx.setBounds(new Rectangle(273, 68, 83, 25));
        btnEx.setForeground(Color.blue);
        btnEx.setText("退出");
        btnEx.addActionListener(new FrmClient_btnEx_actionAdapter(this));
        contentPane.add(lblIP);
        contentPane.add(txtIP);
        contentPane.add(lblPort);
        contentPane.add(txtPort);
        contentPane.add(btnConnection);
        contentPane.add(jScrollPane1);
        contentPane.add(jScrollPane2);
        contentPane.add(btnSend);
        contentPane.add(btnExit);
        contentPane.add(btnEx);
        jScrollPane2.getViewport().add(txtContent);
        jScrollPane1.getViewport().add(txtMessage);
        this.setResizable(false);
        //取消关闭
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
    }

    public void btnConnection_actionPerformed(ActionEvent e) {
        try{
            String ip = txtIP.getText().trim();
            int port = Integer.parseInt(txtPort.getText().trim());
            s = new Socket(ip, port);
            txtMessage.setText("连接成功!本地IP:"+s.getInetAddress().getHostName()+"\n");
            this.IsConnectionWithServer=true;
            in=new DataInputStream(s.getInputStream());
            out=new DataOutputStream(s.getOutputStream());
            new Thread()
            {
                public void run()
                {
                    try {
                        String time=null;
                        while ((s1 = in.readUTF()) != null) {
                            time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                            txtMessage.append("服务器说: "+time+"\n"+ s1 + "\n");
                        }
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null,"接收服务器信息失败!");
                    }
                }
            }.start();
        }catch(NumberFormatException nume)
        {
            JOptionPane.showMessageDialog(this,"端口号请填写数字!");
            txtIP.setText(null);
            txtPort.setText(null);
            txtPort.grabFocus();
            return;
        } catch (IOException ex)
        {
            JOptionPane.showMessageDialog(this,"服务器未启动!");
            txtIP.setText(null);
            txtPort.setText(null);
            txtIP.grabFocus();
        }
    }

    public void btnExit_actionPerformed(ActionEvent e) {
        try{
            String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            out.writeUTF("客户端 "+time+"\n"+s.getInetAddress().getHostName()+"退出");
            s.close();
            System.out.println("端开连接!");
            txtMessage.append("断开连接!\n");
        }catch(IOException ie)
        {
            ;
        }catch(Exception ees)
        {;}
    }
     public static void main(String [] arg)
     {
         FrmClient client=new FrmClient();
         client.setSize(500,500);
         client.setVisible(true);
     }
    public void btnSend_actionPerformed(ActionEvent e) {
        if(this.IsConnectionWithServer)
        {
        if(txtContent.getText().trim().length()==0)
        {
            JOptionPane.showMessageDialog(this,"发送信息请不要为空!");
            return;
        }
        try {
            out.writeUTF(txtContent.getText());
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this,"发送失败!");
        }catch(Exception eeee)
        {
            ;
        }
        txtMessage.append("客户端说:"+txtContent.getText()+"\n");
        txtContent.setText(null);
    }
    else
    {
        JOptionPane.showMessageDialog(this,"未连接服务器!");
        return;
    }
    }

    public void btnEx_actionPerformed(ActionEvent e) {
        try {
            out.writeUTF("客户端退出!");
            this.IsConnectionWithServer=false;
        } catch (IOException ex) {
        }catch(Exception e3)
        {;}
        dispose();
    }
}


class FrmClient_btnEx_actionAdapter implements ActionListener {
    private FrmClient adaptee;
    FrmClient_btnEx_actionAdapter(FrmClient adaptee) {
        this.adaptee = adaptee;
    }

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


class FrmClent_btnSend_actionAdapter implements ActionListener {
    private FrmClient adaptee;
    FrmClent_btnSend_actionAdapter(FrmClient adaptee) {
        this.adaptee = adaptee;
    }

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


class FrmClent_btnExit_actionAdapter implements ActionListener {
    private FrmClient adaptee;
    FrmClent_btnExit_actionAdapter(FrmClient adaptee) {
        this.adaptee = adaptee;
    }

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


class FrmClent_btnConnection_actionAdapter implements ActionListener {
    private FrmClient adaptee;
    FrmClent_btnConnection_actionAdapter(FrmClient adaptee) {
        this.adaptee = adaptee;
    }

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

⌨️ 快捷键说明

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