import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;
public class Client extends JFrame implements Runnable,ActionListener
{ //客户端程序
//创建Socket通信端口号常量
public static final int PORT = 8765;
//创建客户端界面面板
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
//创建布局管理方式
BorderLayout borderLayout1 = new BorderLayout();//边框布局
JScrollPane jScrollPane2 = new JScrollPane();
FlowLayout flowLayout1 = new FlowLayout();//流动布局
GridLayout gridLayout1 = new GridLayout();//网格布局
//创建客户端界面中的按钮
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
//创建用户名录入对话框
JTextField jTextField1 = new JTextField();
//创建聊天对话显示区域
JTextArea jTextArea1 = new JTextArea();
//创建发言输入框
JTextField jTextField2 = new JTextField();
//创建服务器ip地址输入框
JTextField jTextField3 = new JTextField();
//创建服务器端口号输入框
JTextField jTextField4 = new JTextField();
//创建标签对象
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
Socket socket; //声明套接字对象
Thread thread; //声明线程对象
//声明客户器端数据输入输出流
DataInputStream in;
DataOutputStream out;
boolean bool=false;
//声明字符串,name存储用户名,chat_txt存储发言信息,chat_in存储从服务器接收到的信息
String name,chat_txt,chat_in;
String ip=null;
public Client()
{
super("Client");
//设置内容面板无布局方式
getContentPane().setLayout(null);
//初始化客户端界面中的各个组件,包括位置、大小以及处初始显示内容
//初始化输入框组件
jTextArea1.setText("");
jTextField1.setText("用户名输入");
jTextField1.setBounds(new Rectangle(103, 4, 77, 25));
jTextField2.setText("");
jTextField2.setBounds(new Rectangle(4, 6, 311, 22));
jTextField3.setText("");
jTextField4.setText("8765");
//初始化按钮组件
jButton1.setBounds(new Rectangle(190, 4, 100, 25));
jButton1.setToolTipText("");
jButton1.setText("进入聊天室");
jButton1.addActionListener(this);
jButton2.setBounds(new Rectangle(322, 5, 73, 25));
jButton2.setText("发送");
jButton2.addActionListener(this);
jButton3.setBounds(new Rectangle(302, 5, 97, 24));
jButton3.setText("退出聊天室");
jButton3.addActionListener(this);
//初始化面板组件
jPanel1.setLayout(null);
jPanel1.setBounds(new Rectangle(0, 0, 400, 35));
jPanel2.setBounds(new Rectangle(264, 35, 136, 230));
jPanel2.setLayout(gridLayout1);
jPanel3.setBounds(new Rectangle(0, 35, 255, 230));
jPanel3.setLayout(borderLayout1);
jPanel4.setBounds(new Rectangle(0, 265, 400, 35));
jPanel4.setLayout(null);
//初始化标签组件
jLabel1.setText("用户名");
jLabel1.setBounds(new Rectangle(46, 5, 109, 23));
jLabel2.setText("服务器地址");
jLabel3.setText("端口");
//添加组件至指定面板中的固定位置
gridLayout1.setColumns(1);
gridLayout1.setRows(4);
this.getContentPane().add(jPanel1, flowLayout1);
this.getContentPane().add(jPanel2, gridLayout1);
this.getContentPane().add(jPanel4, flowLayout1);
this.getContentPane().add(jPanel3, flowLayout1);
jPanel1.add(jLabel1);
jPanel1.add(jTextField1, null);
jPanel1.add(jButton1, null);
jPanel1.add(jButton3);
jPanel2.add(jLabel2);
jPanel2.add(jTextField3);
jPanel2.add(jLabel3);
jPanel2.add(jTextField4);
jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);
jScrollPane2.getViewport().add(jTextArea1);
jPanel4.add(jTextField2);
jPanel4.add(jButton2);
this.setSize(400,400);
this.setVisible(true);
}
public static void main(String[] args)
{
Client client = new Client();
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run() //客户端线程启动后的动作
{
while(true) //循环执行
{
try
{
//读取服务器发送来的数据信息,并显示在对跨窗口中
chat_in = in.readUTF();
jTextArea1.append(chat_in);
}
catch(IOException e){}
}
}
//界面按钮的事件处理机制
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jButton1)
{
name=jTextField1.getText();
ip=jTextField3.getText();
if(name!="用户名输入"&&ip!=null)
{
try
{
//创建Socket对象
socket = new Socket(ip, PORT);
//创建一个流套接字并将其连接到指定 IP 地址的指定端口号。
//创建客户端数据输入输出流,用于对服务器端发送或接收数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e1) {System.out.println("can not connect");}
thread = new Thread(this);
thread.start();
bool = true;
}
}
else if(e.getSource() == jButton2)
{
chat_txt=jTextField2.getText();
if(chat_txt!=null)
{
//发言,向服务器发送发言的信息
try{out.writeUTF(jTextField1.getText()+"对大家说:"+chat_txt+"\n");}
catch(IOException e2){}
}
else
{
try{out.writeUTF("请说话");}
catch(IOException e3){}
}
}
else if(e.getSource() == jButton3)
{
if(bool==true)
{
try
{
socket.close();
} catch (IOException e4) {}
}
thread.destroy();
bool=false;
this.setVisible(false);
}
}
}