目录
- GUI页面实现技术:①布局管理器-BorderLayout(边界布局管理器) ②FlowLayout(流式布局管理器)
- 类的继承
- 多线程
- java监听器:ActionListener(事件监听器),WindowListener(窗口监听器)
- 构造方法
- String类
- TCP通信三次握手建立连接,四次挥手断开连接
- IO流:BufferedReader(字符读入流),PrintWriter(字符打印流),InputStreamReader,OutputStreamWriter(转换流)
- 这个项目不是我的原创,是基于广东邮电职业技术学院gupt的王玲导师版本改进的
- 基于eclicpe实现
一.建包
本项目需要客户端和服务器
二.客户端的GUI设计
1.实现一个空白窗口
不要把代码一股脑写在main方法
这是登录页
public class LoginFace {
public void makeFace(){
JFrame frame = new JFrame("我的QQ");//创建一个窗口
frame.setSize(400,200);//设置窗口的大小
frame.setLocationRelativeTo(null);//设置窗口居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口每次关闭后程序结束
frame.setVisible(true);//设置窗口的可视化
}
}
2.另起一个类写main方法
public class Login {
public static void main(String[] args) {
new LoginFace().makeFace();//这里是直接调用,直接调用格式:类名.方法名。
}
}
这是聊天页,方法与登录页一样
public class TalkFace {
public void makeFace(){
JFrame frame = new JFrame("聊天");//创建一个窗口
frame.setSize(400,800);//设置窗口的大小
frame.setLocationRelativeTo(null);//设置窗口居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口每次关闭后程序结束
frame.setVisible(true);//设置窗口的可视化
}
}
3.javaGUI的设计
因为Java不适合写页面,所以这里的页面较为简单
下面是登录界面GUI设计的代码较为简单,每行都打了注释
public class LoginFace extends JFrame {// 让页面继承面板类
// 成员变量-在方法外定义组件-对组件进行封装
private JButton bLogin = new JButton("登录");
private JPasswordField tPassword = new JPasswordField();
private JButton bRegist = new JButton("注册");
private JLabel lUsername = new JLabel("账户:");
private JLabel lPassword = new JLabel("密码:");
private JTextField tUsername = new JTextField();
public void makeFace() {// 创建一个普通方法,方便在main方法调用
JPanel top = new JPanel();//这是顶部面板
top.setLayout(null);//取消默认布局,让程序员自定义布局
//自定义布局
lUsername.setBounds(50, 20, 60, 30);
lPassword.setBounds(50, 80, 60, 30);
tUsername.setBounds(100, 20, 200, 30);
tPassword.setBounds(100, 80, 200, 30);
//美化面板
Font font = new Font("隶书", Font.BOLD, 18);//设置字体样式 Font的格式:Font font = new Font("字形",样式, 大小);
top.setBackground(Color.BLUE);//将顶部面板设置为蓝色
//账户的样式
lUsername.setForeground(Color.WHITE);//设置账户的字体色
lUsername.setFont(font);//设置账户的字体样式
//密码的样式
lPassword.setForeground(Color.WHITE);//设置密码的字体色
lPassword.setFont(font);//设置密码的字体样式
//将组件添加到顶部面板
top.add(lUsername);
top.add(lPassword);
top.add(tUsername);
top.add(tPassword);
JPanel bottom = new JPanel();//这是底部面板
bottom.setLayout(new FlowLayout());//将底部面板设置为流式布局
bottom.setBackground(Color.WHITE);//将底部面板设置为白蓝色
bLogin.setForeground(Color.WHITE);//设置登录按钮的字体颜色为白色
bLogin.setBackground(Color.BLUE);//设置登录按钮的背景颜色为蓝色
bRegist.setForeground(Color.WHITE);//设置注册登录按钮的字体颜色为白色
bRegist.setBackground(Color.BLUE);//设置注册登录按钮的背景颜色为蓝色
//将组件添加到底部面板
bottom.add(bRegist);
bottom.add(bLogin);
this.add(top, BorderLayout.CENTER);//将顶部面板添加到面板的中心
this.add(bottom, BorderLayout.SOUTH);//将底部面板添加到面板的南部
// 基本的空白窗口
this.setTitle("我的QQ");// 给窗口命名
this.setSize(400, 200);// 设置窗口的大小
this.setLocationRelativeTo(null);// 设置窗口居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口每次关闭后程序结束
this.setVisible(true);// 设置窗口的可视化
}
}
这是聊天界面的GUI设计-与登录界面相同,所以不打注释
public class TalkFace extends JFrame {
private JTextArea tContent = new JTextArea();
private JTextField tMessage = new JTextField();
private JButton bOk = new JButton("发送");
private JButton bCancel = new JButton("取消");
public void makeFace() {
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add(tContent, BorderLayout.CENTER);
top.add(tMessage, BorderLayout.SOUTH);
JPanel bottom = new JPanel();
bottom.add(bCancel);
bottom.add(bOk);
this.add(top, BorderLayout.CENTER);
this.add(bottom, BorderLayout.SOUTH);
// 基本的空白窗口
this.setTitle("我的QQ");// 给窗口命名
this.setSize(400, 200);// 设置窗口的大小
this.setLocationRelativeTo(null);// 设置窗口居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口每次关闭后程序结束
this.setVisible(true);// 设置窗口的可视化
}
}
三.开始在客户端写代码
1..让登录界面继承ActionListener事件监听器
public class LoginFace extends JFrame implements ActionListener{//让页面继承面板类//让面板在继承事件监听器
2.给组件添加事件监听器
//给组件添加事件监听器
bLogin.addActionListener(this);
bRegist.addActionListener(this);
tPassword.addActionListener(this);
只要按到登录按钮或者是在密码框回车,就在控制台输出登录,否则输出注册
public void actionPerformed(ActionEvent e) {//这是ActionListene未实现的方法
Object source = e.getSource();//获取事件源
if(source==bLogin||source==tPassword) {
System.out.println("登录");
}else {
System.out.println("注册");
}
}