package client;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class Client extends JFrame {
Socket clientsocket = null;
DataInputStream in = null;
DataOutputStream out = null;
JTextArea inputText;
String SerAddress = "192.168.1.100";
int SendPort = 8888;
JTextField NickName;
JTextArea textShow;
JButton button, setbutton;
public Client() { // 构造函数,创建一个布局并初始化
init();
setVisible(true);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setBounds(480, 160, 340, 460);
setTitle("好好学习天天向上聊天室");
setResizable(false);
}
void init() { // 初始化函数,设置布局并且设置监视器
inputText = new JTextArea(4, 29);
button = new JButton(" 发送 ");
JLabel label = new JLabel("昵称");
setbutton = new JButton(" 登录 ");
textShow = new JTextArea(15, 29);
textShow.setEditable(false);
NickName = new JTextField(10);
inputText.setBackground(new Color(45, 210, 209));
setLayout(new FlowLayout());
getContentPane().setBackground(new Color(20, 85, 237));
add(new JScrollPane(textShow));
textShow.setBackground(new Color(45, 210, 209));
setbutton.setBackground(new Color(236, 134, 21));
button.setBackground(new Color(236, 134, 21));
NickName.setBackground(new Color(45, 210, 209));
label.setForeground(new Color(243, 243, 14));
add(label);
add(NickName);
add(setbutton);
add(new JScrollPane(inputText));
add(button);
setbutton.addActionListener(new ActionListener() { //添加监视器
public void actionPerformed(ActionEvent e) {
Thread readData;
Read read = null;
try {
clientsocket = new Socket();
read = new Read();
readData = new Thread(read);
if (clientsocket.isConnected()) {
} else {
InetAddress address = InetAddress.getByName(SerAddress);
InetSocketAddress socketAddress = new InetSocketAddress(
address, SendPort);
clientsocket.connect(socketAddress);
textShow.append(new java.text.SimpleDateFormat(
"yy-MM-dd HH:mm:ss").format(new Date())
+ "
与服务器连接成功
已登录聊天室
");
in = new DataInputStream(clientsocket.getInputStream());
out = new DataOutputStream(clientsocket
.getOutputStream());
read.setDataInputStream(in);
readData.start();
}
} catch (Exception e1) {
textShow.append(new java.text.SimpleDateFormat(
"yy-MM-dd HH:mm:ss").format(new Date())
+ "
服务器连接失败
");
}
}
});
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Send send = new Send();
Thread sendData = new Thread(send);
send.setDataOutputStream(out);
sendData.start();
}
});
addWindowListener(new WindowAdapter() { //响应关闭按钮的功能
public void windowClosing(WindowEvent e) {
int option = JOptionPane
.showConfirmDialog(null, "亲爱的你真的要离开聊天室么?",
" 好好学习,天天向上", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (option == JOptionPane.YES_OPTION)
System.exit(0);
}
});
} // init结束
class Read implements Runnable { //读取输入流的线程
DataInputStream in;
public void setDataInputStream(DataInputStream in) {
this.in = in;
}
public void run() {
String result;
while (true) {
try {
result = in.readUTF();
textShow.append(new java.text.SimpleDateFormat(
"yy-MM-dd HH:mm:ss").format(new Date())
+ "
"
+ result);
} catch (IOException e) {
textShow.append(new java.text.SimpleDateFormat(
"yy-MM-dd HH:mm:ss").format(new Date())
+ "
与服务器断开连接
");
break;
}
}
}
}
class Send implements Runnable { // 发送消息的输出流线程
DataOutputStream out;
public void setDataOutputStream(DataOutputStream out) {
this.out = out;
}
public void run() {
String message = null;
message = NickName.getText() + ":" + inputText.getText() + "
";
try {
out.writeUTF(message);
inputText.setText("");
} catch (Exception e2) {
textShow.append("发送失败:未连接到服务器
");
}
}
}
public static void main(String args[]) {
Client client = new Client();
}
}