JavaSE GUI QQ实现通信

目录

 

前言

  1. GUI页面实现技术:①布局管理器-BorderLayout(边界布局管理器) ②FlowLayout(流式布局管理器)
  2. 类的继承
  3. 多线程
  4. java监听器:ActionListener(事件监听器),WindowListener(窗口监听器)
  5. 构造方法
  6. String类
  7. TCP通信三次握手建立连接,四次挥手断开连接
  8. IO流:BufferedReader(字符读入流),PrintWriter(字符打印流),InputStreamReader,OutputStreamWriter(转换流)
  9. 这个项目不是我的原创,是基于广东邮电职业技术学院gupt的王玲导师版本改进的
  10. 基于eclicpe实现

     

一.建包

本项目需要客户端和服务器

959000708209cfafa3ca43fd0892075d.png

二.客户端的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的设计

9b743bdaa476724248562bf3e127f8e8.png

 

 

9366a093c330484cbe5619e828674502.png

因为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("注册");
		}
		
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值