老师讲课的时候都写好了一部分代码,可是我没有啊
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
/**
* 客户端窗体
*/
public class ClientFrame extends JFrame {
private JPanel contentPane;
private JLabel lblUserName;// 显示用户名
private JTextField tfMessage; // 信息发送文本框
private JButton btnSend;// 发送按钮
private JTextArea textArea;// 信息接收文本域
private String userName;// 用户名称
private ChatRoomClient client;// 客户端连接对象
/**
* 客户端窗体的构造方法
*
* @param ip
* 服务器IP地址
* @param userName
* 用户名
*/
public ClientFrame(String ip, String userName) {
this.userName = userName;
try {
client = new ChatRoomClient(ip, 4569);// 创建客户端对象
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ReadMessageThread messageThread = new ReadMessageThread();// 创建读取客户端消息的线程类对象
messageThread.start();// 启动读取客户端消息的线程类对象
init();// 添加窗体初始化内容
addListener();// 添加窗体监听内容
}
private void init() {
setTitle("客户端");
setResizable(false);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(5, 5, 434, 229);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
textArea.setEditable(false);// 文本域不可编辑
JPanel panel = new JPanel();
panel.setBounds(5, 235, 434, 32);
contentPane.add(panel);
panel.setLayout(null);
lblUserName = new JLabel(userName);
lblUserName.setHorizontalAlignment(SwingConstants.TRAILING);
lblUserName.setBounds(2, 4, 55, 22);
panel.add(lblUserName);
tfMessage = new JTextField();
tfMessage.setBounds(62, 5, 274, 22);
tfMessage.setColumns(10);
panel.add(tfMessage);
btnSend = new JButton("发送");
btnSend.setBounds(336, 4, 93, 23);
panel.add(btnSend);
tfMessage.validate();
}
private void addListener() {
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date date = new Date();// 创建时间类
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// 设定日期格式
client.sendMessage(userName + " " + df.format(date) + ":\n " + tfMessage.getText());// 向服务器发送文本内容
tfMessage.setText("");// 输入框为空
}
});
this.addWindowListener(new WindowAdapter() {// 开启窗口监听
public void windowClosing(WindowEvent atg0) {// 窗口关闭时
int op = JOptionPane.showConfirmDialog(ClientFrame.this, "确定要退出聊天室吗?", "确定", JOptionPane.YES_NO_OPTION);// 弹出提示框
if (op == JOptionPane.YES_OPTION) {// 如果选择是
client.sendMessage("%EXIT%:" + userName);// 发送消息
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
client.close();// 关闭客户端连接
System.exit(0);// 关闭程序
}
}
});
}
/**
* 读取客户端消息的线程类
*/
private class ReadMessageThread extends Thread {
public void run() {// 线程主方法
while (true) {// 无限循环
String str = client.reciveMessage();// 客户端收到服务器发来的文本内容
textArea.append(str + "\n");// 向文本框添加文本内容
}
}
}
}
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.*;
/**
* 连接服务器窗体
*/
@SuppressWarnings("serial") // 通过注解抑制编译器警告
public class LinkServerFrame extends JFrame {
private JPanel contentPane;
private JLabel lblIP;
private JLabel lblUserName;
private JTextField tfIP;
private JTextField tfUserName;
private JButton btnLink;
/**
* 连接服务器窗体的构造方法
*/
public LinkServerFrame() {
setTitle("连接服务器");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 390, 150);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
lblIP = new JLabel("服务器IP地址:");
lblIP.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lblIP.setBounds(20, 15, 100, 15);
contentPane.add(lblIP);
tfIP = new JTextField("127.0.0.1");
tfIP.setBounds(121, 13, 242, 21);
contentPane.add(tfIP);
tfIP.setColumns(10);
lblUserName = new JLabel("用户名:");
lblUserName.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lblUserName.setBounds(60, 40, 60, 15);
contentPane.add(lblUserName);
tfUserName = new JTextField();
tfUserName.setBounds(121, 42, 242, 21);
contentPane.add(tfUserName);
tfUserName.setColumns(10);
btnLink = new JButton("连接服务器");
btnLink.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_btnLink_actionPerformed(e);
}
});
btnLink.setFont(new Font("微软雅黑", Font.PLAIN, 14));
btnLink.setBounds(140, 80, 120, 23);
contentPane.add(btnLink);
}
public static void main(String[] args) {
LinkServerFrame linkServerFrame = new LinkServerFrame();// 创建连接服务器窗体对象
linkServerFrame.setVisible(true);// 显示连接服务器窗体
}
protected void do_btnLink_actionPerformed(ActionEvent e) {
if (!tfIP.getText().equals("") && !tfUserName.getText().equals("")) {// 文本框中的内容不为空时
dispose();// 销毁当前窗体
ClientFrame clientFrame = new ClientFrame(tfIP.getText().trim(), tfUserName.getText().trim());// 创建客户端窗体对象并传参
clientFrame.setVisible(true);// 显示客户端窗体
} else {
JOptionPane.showMessageDialog(null, "文本框里的内容不能为空!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}