⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 framemain.java

📁 用JAVA写的聊天室程序
💻 JAVA
字号:
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class FrameMain extends JFrame implements ActionListener	//客户端主界面
{
	Socket 				toServer;
	ObjectOutputStream	sToServer;
	ObjectInputStream	fromServer;
	cConnect			ServerCon;//自定义类,用于保持与服务器间的连接
	
	String				ServerAddr;
	
	JLabel				L1;
	JLabel				L2;
	JLabel				L3;
	
	JTextField			T1;
	JPasswordField		P1;
	JPasswordField		P2;
	JButton				B1;
	JButton				B2;
	JButton				B3;
	
	JTextArea			Tsend;
	
	Font 				F1;
	
	int lay;
	
	public FrameMain()
	{
		this.setTitle("ChatRoom");
		this.getContentPane().setLayout(null);
		//设置窗口为不使用布局管理器,个人感觉布局管理器设计得太复杂,不易使用
		
		this.getContentPane().setBackground(Color.lightGray);//设置窗口背景色
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		this.setResizable(false);//设置窗口大小用户不可拉伸
		
		F1=new Font("宋体",0,14);
		
		{
			L1=new JLabel();
			L2=new JLabel();
			L3=new JLabel();
			
			L1.setFont(F1);//设置字体
			L2.setFont(F1);
			L3.setFont(F1);
			
			this.add(L1);
			this.add(L2);
			this.add(L3);
		}
		
		{
			T1=new JTextField();
			this.add(T1);
		}
		
		{
			P1=new JPasswordField();
			P2=new JPasswordField();
			
			this.add(P1);
			this.add(P2);
		}
		
		{
			B1=new JButton();
			B2=new JButton();
			B3=new JButton();
			
			B1.addActionListener(this);
			B2.addActionListener(this);
			B3.addActionListener(this);
			
			this.add(B1);		
			this.add(B2);
			this.add(B3);
		}
		
		{
			Tsend=new JTextArea();
			this.add(Tsend);
		}
		
		ServerCon=new cConnect();

		this.add(ServerCon.TshowScro);
		this.add(ServerCon.OnlineScro);
		this.add(ServerCon.MPrivate);
		
		/*上面三个对象在cConnect类中创建,声明如下:
		JScrollPane			TshowScro;
		JScrollPane			OnlineScro;
		JCheckBox			MPrivate;*/
		
		ConUI();//调用界面部署函数

	}
	
	void ConUI()//部署为连接界面
	{
		this.lay=0;//定义此界面为顶层(自定义属性)
		this.setVisible(false);//将窗口设为不可见,以便更改组件布局时因延时而显得不自然
		this.setLocation(300,250);//设置窗口坐标
		this.setSize(400,200);//设置窗口大小
		this.setTitle("ChatRoom - 连接");//设置窗口标题
		
		//部署组件
		{
			L1.setText("服务器地址:");
			L1.setBounds(80,40,80,20);//设置组件四围大小,setBounds(left,top,width,height)
			L1.setVisible(true);
		}
		
		{
			T1.setText(null);
			T1.setBounds(160,40,150,20);
			T1.setVisible(true);
		}
		
		{
			B1.setText("连接");
			B1.setBounds(100,100,80,40);
			B1.setVisible(true);
		}
		
		{
			B2.setText("退出");
			B2.setBounds(220,100,80,40);			
			B2.setVisible(true);
		}
		this.setVisible(true);//部署完毕,重新将窗口设为可见
	}
	
	
	
	void LogonUI()//部署为登陆界面
	{
		lay++;
		this.setVisible(false);
		this.setLocation(300,200);
		this.setSize(400,300);
		this.setTitle("ChatRoom - 登陆");
		L3.setVisible(false);
		P2.setVisible(false);
		{
			L1.setText("用户名:");
			L1.setBounds(100,60,80,20);
			L1.setVisible(true);
		}
				
		{
			L2.setText("密码:");
			L2.setBounds(100,100,80,20);
			L2.setVisible(true);
		}
		
		{
			T1.setText(null);
			T1.setBounds(160,60,120,20);
			T1.setVisible(true);
		}
		
		{
			P1.setText(null);
			P1.setBounds(160,100,120,20);
			P1.setVisible(true);
		}
				
		{
			B1.setText("登录");
			B1.setBounds(50,160,80,40);
			B1.setVisible(true);
		}
		{
			B2.setText("注册");
			B2.setBounds(160,160,80,40);
			B2.setVisible(true);
		}
				
		{
			B3.setText("退出");
			B3.setBounds(270,160,80,40);
			B3.setVisible(true);
		}
		
		this.setVisible(true);
	}
	
	
	void RegUI()//部署为注册界面
	{
		lay++;
		this.setVisible(false);
		this.setSize(300,400);
		this.setTitle("ChatRoom - 注册新用户");
		B3.setVisible(false);
		{
			L1.setText("用户名:");
			L1.setBounds(50,60,80,20);
			L1.setVisible(true);
		}
				
		{
			L2.setText("密码:");
			L2.setBounds(50,120,80,20);
			L2.setVisible(true);
		}
				
		{
			L3.setText("验证密码:");
			L3.setBounds(50,180,80,20);
			L3.setVisible(true);
		}
		
		{
			T1.setText(null);
			T1.setBounds(120,60,120,20);
			T1.setVisible(true);
		}
		
		{
			P1.setText(null);
			P1.setBounds(120,120,120,20);
			P1.setVisible(true);
		}
		
		{
			P2.setText(null);
			P2.setBounds(120,180,120,20);
			P2.setVisible(true);
		}
		
				
		{
			B1.setText("提交");
			B1.setBounds(50,260,80,40);
			B1.setVisible(true);
		}
		{
			B2.setText("返回");
			B2.setBounds(170,260,80,40);
			B2.setVisible(true);
		}
		
		
		this.setVisible(true);
		
	}
	
	void ChatUI()//部署为聊天界面
	{
		lay++;
		this.setVisible(false);
		this.setSize(800,600);
		this.setLocation(100,100);
		this.setTitle("ChatRoom - "+T1.getText());
		L1.setVisible(false);
		L2.setVisible(false);
		L3.setVisible(false);
		T1.setVisible(false);
		P1.setVisible(false);
		P2.setVisible(false);
		
		{
			ServerCon.Tshow.setEditable(false);
			ServerCon.Tshow.setVisible(true);
		}
		
		{
			ServerCon.TshowScro.setBounds(4,4,600,400);
			ServerCon.TshowScro.setVisible(true);
		}

		{
		//	ServerCon.Online.setEditable(true);
			ServerCon.Online.setBounds(610,4,180,400);
			ServerCon.Online.setVisible(true);
		}

		
		{
			ServerCon.OnlineScro.setBounds(610,4,180,400);
			ServerCon.OnlineScro.setVisible(true);
		}
		{
			Tsend.setEditable(true);
			Tsend.setBounds(4,410,600,100);
			Tsend.setVisible(true);
		}
						
		{
			B1.setText("发送消息");
			B1.setBounds(610,410,180,100);
			B1.setVisible(true);
		}

		
		{
			ServerCon.MPrivate.setBackground(Color.lightGray);
			ServerCon.MPrivate.setBounds(200,520,80,40);
			ServerCon.MPrivate.setVisible(true);
		}

								
		{
			B2.setText("退出");
			B2.setBounds(610,520,180,40);
			B2.setVisible(true);
		}
								
		{
			B3.setText("清屏");
			B3.setBounds(100,520,80,40);
			B3.setVisible(true);
		}
		this.setVisible(true);
		
	}
	void BackUI()//返回上一级界面
	{//由于此程序的界面少,层次感不强,只有注册界面应用到,因此该设计属华而不实
		switch(lay)
		{
			case 2:
				LogonUI();
				break;
		}
		lay=lay-2;
	}
	
	void RegOrLogon(JButton e)//验证注册信息并提交服务器
	{
			String UName=T1.getText().trim();
			String UPassword1=new String(P1.getPassword());
			String UPassword2=new String(P2.getPassword());
			
			if(UName.length()==0)
			{
				JOptionPane.showMessageDialog(null,"用户名不能为空");
				return;
			}
			
			if(UPassword1.length()==0)
			{
				JOptionPane.showMessageDialog(null,"密码有误");
				return;
			}
			
			if(!UPassword1.equals(UPassword2)&&P2.isVisible())//boolean isVisible()::用于返回组件的可见状态
			{
				JOptionPane.showMessageDialog(null,"密码输入不一致");
				return;
			}
			
			Message Uinfo=new Message(new Users(),"",-1);
			Uinfo.user.Name=UName;
			Uinfo.user.Password=UPassword1;
			
			if(e.getText().equals("提交"))
			{
				Uinfo.type=2;
			}
			else
			{
				Uinfo.type=3;
			}
			if(ServerCon.SendInfo(Uinfo))
			{
				if(Uinfo.type==3)
				{
					ChatUI();
				}
			}
	}
	
	
	public void actionPerformed(ActionEvent evt)//按扭单击事件
	{
		JButton Bevt=(JButton)evt.getSource();
		
		if(Bevt.getText()=="连接")
		{
			if(ServerCon.ConToServer(T1.getText()))
			{
				LogonUI();
			}
			return;//退出此事件函数,以节省资源不再执行以下的判断语句
		}
		
		if(Bevt.getText()=="注册")
		{
			RegUI();
			return;
		}
		
		if(Bevt.getText()=="提交"||Bevt.getText()=="登录")
		{
			RegOrLogon(Bevt);
			return;
		}
		
		if(Bevt.getText()=="返回")
		{
			BackUI();
			return;
		}
		
		if(Bevt.getText()=="发送消息")
		{
			if(ServerCon.SendMsg(Tsend.getText()))
				Tsend.setText(null);//清空输入栏
			return;
		}
		
		if(Bevt.getText()=="清屏")
		{
			ServerCon.Tshow.setText(null);
			return;
		}
		
		if(Bevt.getText()=="退出")
		{
			System.exit(0);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -