📄 client.java
字号:
import java.net.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JPasswordField;
import java.util.*;
import java.text.*; //包含DateFormat,SimpleDateFormat时间类
//主类
class Client extends JFrame implements ActionListener{
public static user [] u = new user[2];//用数组存储两个用户
DataOutputStream dos;
DataInputStream dis;
TextArea input;
TextArea message;
Socket s1;
JPanel p1;//定义面板
JPanel p2;
public Client(String mingzi)
{
//设计流式布局聊天界面,
super("与"+mingzi+"聊天中");
JOptionPane.showMessageDialog(null,"进入QQ聊天界面");
Container c = getContentPane();//获取Container对象
input = new TextArea(4,40);//输入文本框
message = new TextArea(15,40);//显示消息文本框
MenuBar m = new MenuBar();
setMenuBar(m);
Menu ceshi = new Menu("测试连接");
Menu ziti = new Menu("字体");
Menu beijing = new Menu("背景色");
m.add(ceshi);
m.add(ziti);
m.add(beijing);
MenuItem lianjie = new MenuItem("连接服务器");
MenuItem zhongduan = new MenuItem("中断服务器连接");
MenuItem xuanze = new MenuItem("颜色选项");
MenuItem zitiyanse = new MenuItem("字体颜色");
Menu zitileixing = new Menu("字体类型");
MenuItem songti = new MenuItem("宋体");
MenuItem kaiti = new MenuItem("TimesRoman");
MenuItem liti = new MenuItem("隶体");
zitileixing.add(songti);
zitileixing.addSeparator();
zitileixing.add(kaiti);
zitileixing.addSeparator();
zitileixing.add(liti);
ceshi.add(lianjie);
ceshi.addSeparator();
ceshi.add(zhongduan);
beijing.add(xuanze);
ziti.add(zitileixing);
ziti.addSeparator();
ziti.add(zitiyanse);
JButton guanbi=new JButton("关闭");
JButton fasong=new JButton("发送");
p1=new JPanel();
p1.add(fasong);
p1.add(guanbi);
p2=new JPanel();
p2.add(message);
p2.add(input);
p2.add(p1);
p2.setBackground(Color.blue);
c.setLayout(new BorderLayout());
c.add("Center",p2);
//给部件注册事件监听器
guanbi.addActionListener(this);
fasong.addActionListener(this);
lianjie.addActionListener(this);
zhongduan.addActionListener(this);
xuanze.addActionListener(this);
zitiyanse.addActionListener(this);
songti.addActionListener(this);
kaiti.addActionListener(this);
liti.addActionListener(this);
setSize(450,470);
//实现窗体的交互形式关闭
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(Client.this,"真的要退出吗?",
"结束程序",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE))
{
Window w=e.getWindow();
w.dispose();
System.exit(0);
}
}
});
}
//实现监听者的方法
public void actionPerformed(ActionEvent e)
{
String comp=e.getActionCommand();
if(comp.equals("关闭"))
{
dispose();
System.exit(0);
}
//发送数据
else if(comp.equals("发送"))
{
//Date dat = new Date();
//显示指定时间格式
DateFormat df = new SimpleDateFormat("(yyyy-MM-dd HH:mm:ss)");
String str = df.format(new Date());
//昵称
String s1 = u[denglu.i].nicheng +" "+str;
String s2 = input.getText();
try
{
dos.writeUTF(s1);
dos.writeUTF(s2);//发送数据到服务器
input.replaceRange("",0,160);//清空文本域的内容
}catch(Exception o){}
}
//实现测试连接功能
else if(comp.equals("连接服务器"))
{
try
{
s1 = new Socket("localhost",5432);//连接服务器
dos = new DataOutputStream(s1.getOutputStream());//取得soket输出流
dis = new DataInputStream(s1.getInputStream());
}catch(Exception p){}
new Thread (new receiveThread(dis,message)).start(); //创建一个接收数据线程
JOptionPane.showMessageDialog(null,"连接成功!");
try
{
dos.writeUTF(u[denglu.i].nicheng+"上线了有人在不?");
}catch(Exception p){}
}
else if(comp.equals("中断服务器连接"))
{
try
{
JOptionPane.showMessageDialog(null,"已断开连接!");
dos.writeUTF(u[denglu.i].nicheng+"下线了");
s1.close();
}catch(Exception p){}
}
//改变背景色
else if(comp.equals("颜色选项"))
{
Color boardColor = JColorChooser.showDialog(Client.this,"聊天背景颜色",
Client.this.getBackground());
p1.setBackground(boardColor);
p2.setBackground(boardColor);
}
//改变字体颜色
else if(comp.equals("字体颜色"))
{
Color boardColor = JColorChooser.showDialog(Client.this,"选择字体颜色",
Client.this.getBackground());
input.setForeground(boardColor);
message.setForeground(boardColor);
}
//改变字体类型
else if(comp.equals("宋体"))
{
Font f=new Font("宋体",Font.BOLD,17);
input.setFont(f);
message.setFont(f);
}
else if(comp.equals("隶体"))
{
Font f=new Font("隶体",Font.PLAIN,17);
input.setFont(f);
message.setFont(f);
}
else if(comp.equals("TimesRoman"))
{
Font f=new Font("TimesRoman",Font.PLAIN,17);
input.setFont(f);
message.setFont(f);
}
}
public static void main(String args[]) throws Exception
{
//添加两个用户,id与昵称邦定
u[0]=new user(0,"蔡报春");
u[1]=new user(1,"萨达姆");
//显示登录界面
new denglu();
}
}
// 接收消息线程循环读取网络消息,显示在文本域
class receiveThread implements Runnable
{
DataInputStream dis;
TextArea displayarea;
public receiveThread(DataInputStream dis ,TextArea m) {
this.dis=dis;
displayarea=m;
}
public void run() {
for(;;) {
try {
String str = new String (dis.readUTF()); //读取其他客户经服务器转发的消息
displayarea.append(str+"\n"); //将消息添加到文本域显示
} catch (IOException e ){ }
}
}
}
//建立用户类
class user{
int id;
String nicheng;
public user(int id,String nicheng){
this.id=id;
this.nicheng=nicheng;
}
}
//用网格块布局登录界面
class denglu extends JFrame implements ActionListener
{
public static int i = 2;
JTextField QQ;
JTextField password;
JButton login;
JButton cancle;
public denglu()
{
super("QQ用户登录");
JOptionPane.showMessageDialog(null,"进入蔡报春QQ登录界面");
setLayout(new GridBagLayout()); //网格块布局
GridBagConstraints gridBag = new GridBagConstraints();
gridBag.fill = GridBagConstraints.HORIZONTAL;//以水平方式填充布局
gridBag.weightx = 0; //行不自动缩放
gridBag.weighty = 100;//列自动缩放
this.add(new JLabel("QQ号码"),gridBag,0,0,1,1);
QQ = new JTextField(10);
this.add(QQ,gridBag,1,0,2,1);
this.add(new JLabel("QQ密码"),gridBag,0,1,1,1);
password = new JPasswordField(10);
this.add(password,gridBag,1,1,2,1);
login = new JButton("登录",new ImageIcon("login.gif"));
cancle = new JButton("取消",new ImageIcon("2.gif"));
this.add(login,gridBag,0,2,1,1);
this.add(cancle,gridBag,1,2,1,1);
login.addActionListener(this);
cancle.addActionListener(this);
setSize(300,300);
setResizable(false);
setVisible(true);
//交互式关闭窗体
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(denglu.this,"真的要退出吗?",
"结束程序",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE))
{
Window w=e.getWindow();
w.dispose();
System.exit(0);
}
else
{
setVisible(true);
}
}
});
}
//将部件添加到面板指定的位置
private void add(Component c,GridBagConstraints gr,int x,int y,int w,int z)
{
gr.gridx = x;
gr.gridy = y;
gr.gridheight = z;
gr.gridwidth = w;
add(c,gr);
}
//实现登录事件监听者的方法
public void actionPerformed(ActionEvent e)
{
i = Integer.parseInt(password.getText());
if(i == 0)
{
Client liao=new Client(Client.u[1].nicheng);
liao.setVisible(true);
}
else if(i == 1)
{
Client liao=new Client(Client.u[0].nicheng);
liao.setVisible(true);
}
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -