📄 myclient.java
字号:
sp.setBounds(2,20,100,250);
pp.add(sp);
JLabel praUser = new JLabel("聊天对象");
praUser.setFont(ft);
praUser.setBounds(2,0,80,20);
pp.add(praUser);
tp.addTab("私聊区",pp);
lblPri = new JLabel("↙你正在私聊中");
lblPri.setFont(new Font("宋体",1,18));
lblPri.setBorder(border2);
lblPri.setBounds(120,2,150,20);
lblPri.setVisible(false);
panel.add(lblPri);
lblFont = new JLabel("字体"); //字体设置模块
lblFont.setFont(ft);
lblFont.setBounds(540,330,65,30);
lblFontSize = new JLabel("字体大小");
lblFontSize.setFont(ft);
lblFontSize.setBounds(540,360,65,30);
lblFontStyle = new JLabel("字形");
lblFontStyle.setFont(ft);
lblFontStyle.setBounds(540,390,65,30);
lblFontColor = new JLabel("字体颜色");
lblFontColor.setFont(ft);
lblFontColor.setBounds(540,420,65,30);
String f[] = {"宋体","隶属","黑体","仿宋"};
cmbFont = new JComboBox(f);
cmbFont.setFont(ft);
cmbFont.setBounds(600,330,70,30);
String fs[] = {"18","20","22","24","26","28","30","32","34","36","38","40"};
cmbFontSize = new JComboBox(fs);
cmbFontSize.setFont(ft);
cmbFontSize.setBounds(600,360,70,30);
String fsy[] = {"常规","粗体","斜体","粗斜体"};
cmbFontStyle = new JComboBox(fsy);
cmbFontStyle.setFont(ft);
cmbFontStyle.setBounds(600,390,70,30);
String fc[] = {"红色","绿色","蓝色","黄色","黑色"};
cmbFontColor = new JComboBox(fc);
cmbFontColor.setFont(ft);
cmbFontColor.setBounds(600,420,70,30);
panel.add(lblFont); panel.add(cmbFont);
panel.add(lblFontSize); panel.add(cmbFontSize);
panel.add(lblFontStyle); panel.add(cmbFontStyle);
panel.add(lblFontColor); panel.add(cmbFontColor);
txtSend = new JTextArea(4,20); //SEND模块
txtSend.setBackground(new Color(240,240,240));
txtSend.setLineWrap(true);
spSend = new JScrollPane(txtSend);
spSend.setBounds(20,370,350,80);
spSend.setBorder(border);
panel.add(spSend);
btnSend = new JButton("发送");
btnSend.setBounds(410,390,70,35);
panel.add(btnSend);
lblOnline = new JLabel("在线人数"); //user模块
lblOnline.setFont(ft);
lblOnline.setForeground(Color.red);
lblOnline.setBounds(540,20,130,20);
panel.add(lblOnline);
lUser = new JList();
lUser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
lUser.setFont(new Font("宋体",0,20));
lUser.setBackground(new Color(240,240,240));
spList = new JScrollPane(lUser);
spList.setBorder(border);
spList.setBounds(540,40,130,280);
panel.add(spList);
btnSetting = new JButton("个人设置");
btnSetting.setFont(ft);
btnSetting.setBounds(20,330,100,30);
panel.add(btnSetting);
cmbFontStyle.addItemListener(this);
cmbFontSize.addItemListener(this);
cmbFont.addItemListener(this);
cmbFontColor.addItemListener(this);
btnSend.addActionListener(this);
btnSetting.addActionListener(this);
lUser.addMouseListener(this);
praLis.addMouseListener(this);
tp.addMouseListener(this);
frmChat.getContentPane().add(panel);
frmChat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmChat.setSize(700,500);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frmChat.getSize();
frmChat.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
frmChat.setResizable(false);
frmChat.setVisible(true);
}
public void mouseReleased(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mousePressed(MouseEvent me)
{
JTabbedPane t1 = (JTabbedPane)me.getSource();
if(t1.equals(tp))
{
if((me.getX() >= 0 && me.getX() <= 70) && (me.getY() >= 0 && me.getY() <= 20))
{
lblPub.setVisible(true);
lblPri.setVisible(false);
}
else if((me.getX() >= 70 && me.getX() <= 140) && (me.getY() >= 0 && me.getY() <= 20))
{
lblPri.setVisible(true);
lblPub.setVisible(false);
}
}
}
public void mouseClicked(MouseEvent me)
{
JList li = (JList)me.getSource();
if(li.equals(lUser))
{
int clickTimes = me.getClickCount();
if(clickTimes == 2)
{
String us = (String)lUser.getSelectedValue();
vUs.addElement(us);
praLis.setListData(vUs);
}
}
else if(li.equals(praLis))
{
int clickTime = me.getClickCount();
if(clickTime == 2)
{
String uss = (String)praLis.getSelectedValue();
vUs.remove(uss);
praLis.setListData(vUs);
}
}
}
//发送MSG
public void actionPerformed(ActionEvent ev)
{
JButton ba = (JButton)ev.getSource();
try
{
if(ba.equals(btnSend))
{
String strSend = txtSend.getText();
if(strSend.equals(""))
{
JOptionPane.showMessageDialog(frmChat,"发送内容不能未空,请重新输入!");
}
else if(tp.getSelectedIndex() == 0)
{
oos.writeObject(new String("PublicMsg"));
oos.writeObject(userName + " " + "对大家说:" + " " + strSend);
txtSend.setText("");
}
else if(tp.getSelectedIndex() == 1)
{
if(vUs.size() == 0)
{
JOptionPane.showMessageDialog(frmChat,"请先添加聊天对象!");
}
else
{
oos.writeObject(new String("PrivateMsg"));
oos.writeObject(vUs);
oos.writeObject(userName + " " + "说:" + " " + strSend);
txtSend.setText("");
}
}
}
else if(ba.equals(btnSetting))
{
sf = new SettingFrm(usr);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public void itemStateChanged(ItemEvent ie)
{
JComboBox jcb = (JComboBox)ie.getSource();
if(jcb.equals(cmbFontColor))
{
int fc = cmbFontColor.getSelectedIndex();
switch(fc)
{
case 0:
txtSend.setForeground(Color.red);
txtPubMsg.setForeground(Color.red);
txtPraMsg.setForeground(Color.red);
break;
case 1:
txtSend.setForeground(Color.green);
txtPubMsg.setForeground(Color.green);
txtPraMsg.setForeground(Color.green);
break;
case 2:
txtSend.setForeground(Color.blue);
txtPubMsg.setForeground(Color.blue);
txtPraMsg.setForeground(Color.blue);
break;
case 3:
txtSend.setForeground(Color.yellow);
txtPubMsg.setForeground(Color.yellow);
txtPraMsg.setForeground(Color.yellow);
break;
case 4:
txtSend.setForeground(Color.black);
txtPubMsg.setForeground(Color.black);
txtPraMsg.setForeground(Color.black);
break;
}
}
else if(jcb.equals(cmbFont)||jcb.equals(cmbFontStyle)||jcb.equals(cmbFontSize))
{
String f = (String)cmbFont.getSelectedItem();
int ss = cmbFontStyle.getSelectedIndex();
int fs = 0;
switch(ss)
{
case 0:
fs = 0;
break;
case 1:
fs = 1;
break;
case 2:
fs = 2;
break;
case 3:
fs = 3;
break;
}
String strFss = (String)cmbFontSize.getSelectedItem();
int fss = Integer.parseInt(strFss);
Font font = new Font(f,fs,fss);
txtSend.setFont(font);
txtPubMsg.setFont(font);
txtPraMsg.setFont(font);
}
}
}
//***********************************************************************End
//***************************************************************个人设置窗口
class SettingFrm extends EnrolFrm
{
UserData uData;
public SettingFrm(UserData ud)
{
uData = ud;
frmEnrol.setSize(400,250);
frmEnrol.setContentPane(pEnrol);
frmEnrol.setTitle("个人设置");
gdl = new GridLayout(7,2,2,2);
pEnrol.setLayout(gdl);
pEnrol.remove(lblPWD);
pEnrol.remove(lblPWD2);
pEnrol.remove(txtPWD);
pEnrol.remove(txtPWD2);
txtId.setEditable(false);
txtId.setText(uData.id);
txtName.setText(uData.name);
txtAge.setText(uData.age);
txtPhone.setText(uData.phone);
txtMail.setText(uData.mail);
cmbSex.setSelectedItem(uData.sex);
}
public void condition()
{
String id = uData.id;
String pwd = uData.pwd;
String name = txtName.getText();
String sex = (String)cmbSex.getSelectedItem();
String age = txtAge.getText();
String phone = txtPhone.getText();
String mail = txtMail.getText();
try
{
while(true)
{
if(name.equals(""))
{
JOptionPane.showMessageDialog(frmEnrol,"呢称为必填项!");
break;
}
else if(name.length() > 10)
{
JOptionPane.showMessageDialog(frmEnrol,"呢称不能超过10个英文字母、数字或汉字!");
break;
}
else
{
UserData us = new UserData(id,pwd,name,sex,age,phone,mail);
oos.writeObject(new String("Setting"));
oos.writeObject(us);
break;
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
//***********************************************************************End
public void run() //接收线程
{
ChatFrm cf = null;
try
{
while(ois != null)
{
String ms = (String)ois.readObject();
if(ms.equals("LoginOK")) //构建聊天窗口
{
UserData userData = (UserData)ois.readObject();
cf = new ChatFrm(userData);
frm.setVisible(false);
}
else if(ms.equals("LoginFalse"))
{
JOptionPane.showMessageDialog(frm,"没有此帐号或密码不正确!");
}
else if(ms.equals("EnrolOK"))
{
JOptionPane.showMessageDialog(ef.frmEnrol,"注册成功!");
ef.frmEnrol.setVisible(false);
}
else if(ms.equals("EnrolFalse"))
{
JOptionPane.showMessageDialog(ef.frmEnrol,"此帐号已被注册!");
ef.txtId.setText("");
ef.txtPWD.setText("");
ef.txtPWD2.setText("");
}
else if(ms.equals("PublicMsg"))
{
String msg = (String)ois.readObject();
cf.txtPubMsg.append(msg);
}
else if(ms.equals("PrivateMsg"))
{
String pmsg = (String)ois.readObject();
cf.txtPraMsg.append(pmsg);
}
else if(ms.equals("Online"))
{
String us = (String)ois.readObject();
cf.vUser.addElement(us);
int ll = cf.vUser.size();
cf.lUser.setListData(cf.vUser);
String ol = new String("在线人数" + " " + ll);
cf.lblOnline.setText(ol);
}
else if(ms.equals("ReOnline"))
{
Vector ver = (Vector)ois.readObject();
cf.vUser = ver;
int lc =cf.vUser.size();
cf.lUser.setListData(cf.vUser);
String cl = new String("在线人数" + " " + lc);
cf.lblOnline.setText(cl);
}
else if(ms.equals("SettingOK"))
{
JOptionPane.showMessageDialog(sf.frmEnrol, "服务器已经接受你的新资料");
sf.frmEnrol.setVisible(false);
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String mk[])
{
new MyClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -