📄 client.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.util.*;
public class Client extends JFrame implements
ActionListener,Runnable,KeyListener
{
public static int port=8888;
ServerSocket server;
Socket client;
BufferedReader cin;
PrintWriter cout;
private javax.swing.Timer timer=new javax.swing.Timer(1000,this);
static ClientLoad clientload;
//以下为GUI设计变量声明
private Font font1=new Font("宋体",Font.BOLD,17);
private String strUsername=null;
private JLabel welcomeUser=new JLabel();
private JButton exitButton=new JButton("退出");
private JComboBox jcbfont=new JComboBox();
private JComboBox jcbfontsize=new JComboBox();
private JButton jcbmusic=new JButton("停止音乐");
private JButton boldButton=new JButton("B");
private JButton italButton=new JButton("I");
private JButton underButton=new JButton("U");
private JButton colorfor=new JButton("字色");
private JButton colorback=new JButton("背景");
private JPanel jpTop=new JPanel(new BorderLayout());
private JPanel jpAll=new JPanel(new BorderLayout());
JButton messageButton=new JButton("发送消息");
JTextArea clientMessage=new JTextArea(2,30);
private JLabel label1=new JLabel(" 聊天室名单 ");
Vector userName=new Vector();
private JList jlist=new JList(userName);
JPanel jpCenter=new JPanel(new BorderLayout());
JPanel jpBase=new JPanel();
JPanel jpList=new JPanel();
JPanel jpfont=new JPanel();
JPanel jpAdd=new JPanel(new GridLayout(2,1,0,0));
JScrollPane messageScrollPane;
JScrollPane clientScrollPane;
JTextArea message=new JTextArea();
Thread chart=null;
int i=1;
boolean isBold=false;
boolean isItlic=false;
Sound su1=new Sound("Music1.mid");
public Client()
{
super("网络聊天客户端");
Getusername();
AddTop();
AddCenter();
AddSouth();
AddList();
AddFont();
jcbmusic.setSelected(true);
jpAll.add(jpAdd,BorderLayout.SOUTH);
jpAll.add(jpCenter,BorderLayout.CENTER);
jpAll.add(jpTop,BorderLayout.NORTH);
this.getContentPane().add(jpAll,BorderLayout.CENTER);
this.setBackground(Color.red);
this.setSize(700,400);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((d.width - this.getSize().width) / 2,
(d.height - this.getSize().height) / 2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ClientLoad.password.selectAll();
//验证用户名及密码
if(loginServer(ClientLoad.username.getSelectedItem().toString(),
ClientLoad.password.getSelectedText()))
{
chart=new Thread(this);
chart.start();
this.setVisible(true);
timer.start();
}
else{
JOptionPane.showMessageDialog(null,"用户名或密码错误!请重新登录!");
ClientLoad clientload=new ClientLoad();
System.exit(-1);
}
su1.play();
}
public void AddFont()
{
jpfont.setBackground(new Color(118,205,255));
jpBase.setBackground(new Color(118,205,255));
for(int i=8;i<29;i++) jcbfontsize.addItem(String.valueOf(i));
jcbfontsize.setSelectedItem("15");
//获得系统字体类型
GraphicsEnvironment env=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontNames[]=env.getAvailableFontFamilyNames();
for(int i=0;i<fontNames.length;i++)
{
jcbfont.addItem(fontNames[i]);
}
jcbfont.setSelectedItem("宋体");
jpfont.add(jcbfont);
jpfont.add(jcbfontsize);
boldButton.setFont(new Font("SansSerif",Font.BOLD,14));
jpfont.add(boldButton);
italButton.setFont(new Font("SansSerif",Font.ITALIC+Font.BOLD,14));
jpfont.add(italButton);
underButton.setFont(new Font("SansSerif",Font.PLAIN,14));
jpfont.add(underButton);
jpfont.add(colorfor);
jpfont.add(colorback);
jpfont.add(jcbmusic);
jpAdd.add(jpfont);
jpAdd.add(jpBase);
jcbfont.addActionListener(this);
jcbfontsize.addActionListener(this);
underButton.addActionListener(this);
italButton.addActionListener(this);
boldButton.addActionListener(this);
colorback.addActionListener(this);
colorfor.addActionListener(this);
jcbmusic.addActionListener(this);
}
public void AddList()
{
label1.setForeground(new Color(118,205,255));
jpList.setLayout(new BorderLayout());
jpList.add(jlist,BorderLayout.CENTER);
jpList.add(label1,BorderLayout.NORTH);
}
public void AddSouth()
{
clientMessage.addKeyListener(this);
clientScrollPane=new JScrollPane(clientMessage,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpBase.add(clientScrollPane);
jpBase.add(messageButton);
messageButton.addActionListener(this);
}
public void AddCenter()
{
message.setFont(new Font("宋体",Font.PLAIN,15));
messageScrollPane=new JScrollPane(message,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpCenter.add(messageScrollPane,BorderLayout.CENTER);
jpCenter.add(jpList,BorderLayout.EAST);
}
public void AddTop()
{
jpTop.setBackground(new Color(118,205,255));
exitButton.addActionListener(this);
jpTop.add(exitButton,BorderLayout.EAST);
welcomeUser.setFont(font1);
welcomeUser.setForeground(Color.blue);
welcomeUser.setHorizontalAlignment(JLabel.RIGHT);
jpTop.add(welcomeUser,BorderLayout.CENTER);
}
public void Getusername()
{
strUsername=ClientLoad.username.getSelectedItem().toString();
welcomeUser.setText(""+strUsername+"欢迎您来到聊天室!");
}
//以下为KeyListenner接口
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
sendMessage(clientMessage.getText());
message.append("发送:"+clientMessage.getText()+"\n");
}
}
public void actionPerformed(ActionEvent e)
{
if(i%3==1)
{
welcomeUser.setHorizontalAlignment(JLabel.RIGHT);
welcomeUser.setForeground(Color.blue);
}
if(i%3==0)
{
welcomeUser.setHorizontalAlignment(JLabel.LEFT);
welcomeUser.setForeground(Color.red);
}
if(i%3==2)
{
welcomeUser.setHorizontalAlignment(JLabel.CENTER);
welcomeUser.setForeground(Color.white);
}
i++;
if(e.getSource()==exitButton)
{
cout.println("exit");
chart=null;
}
if (e.getSource()==messageButton)
{
sendMessage(clientMessage.getText());
message.append("发送:"+clientMessage.getText()+"\n");
}
if(e.getSource()==jcbfont||e.getSource()==jcbfontsize
||e.getSource()==boldButton)
{
String strSize=null;
String strName=null;
strName=jcbfont.getSelectedItem().toString();
strSize=jcbfontsize.getSelectedItem().toString();
Font font=new Font(strName,Font.PLAIN,Integer.parseInt(strSize));
if(isItlic)
{
if(isBold)
{
font=new Font(strName,Font.ITALIC,Integer.parseInt(strSize));
isBold=false;
}
else
{
font=new Font(strName,Font.ITALIC+Font.BOLD,Integer.parseInt(strSize));
isBold=true;
}
}
else
{
if(isBold)
{
font=new Font(strName,Font.PLAIN,Integer.parseInt(strSize));
isBold=false;
}
else
{
font=new Font(strName,Font.BOLD,Integer.parseInt(strSize));
isBold=true;
}
}
message.setFont(font);
}
if(e.getSource()==italButton)
{
String strSize=null;
String strName=null;
strName=jcbfont.getSelectedItem().toString();
strSize=jcbfontsize.getSelectedItem().toString();
Font font=new Font(strName,Font.PLAIN,Integer.parseInt(strSize));
if(isBold)
{
if(isItlic)
{
font=new Font(strName,Font.BOLD,Integer.parseInt(strSize));
isItlic=false;
}
else
{
font=new Font(strName,Font.ITALIC+Font.BOLD,Integer.parseInt(strSize));
isItlic=true;
}
}
else
{
if(isItlic)
{
font=new Font(strName,Font.PLAIN,Integer.parseInt(strSize));
isItlic=false;
}
else
{
font=new Font(strName,Font.ITALIC,Integer.parseInt(strSize));
isItlic=true;
}
}
message.setFont(font);
}
if(e.getSource()==colorback)
{
Color selectedColor =JColorChooser.showDialog(null, "背景颜色--QQ",
message.getBackground());
if (selectedColor != null)
message.setBackground(selectedColor);
}
if(e.getSource()==colorfor)
{
Color selectedColor =JColorChooser.showDialog(null, "字体颜色--QQ",
message.getForeground());
if (selectedColor != null)
message.setForeground(selectedColor);
}
if(e.getActionCommand()=="停止音乐")
{
su1.stop();
jcbmusic.setText("播放音乐");
}
if(e.getActionCommand()=="播放音乐")
{
su1.play();
jcbmusic.setText("停止音乐");
}
if(e.getActionCommand()=="退出")
{
System.exit(0);
}
}
public boolean loginServer(String username,String password)
{
try{
client=new Socket(ClientLoad.ServerIP.getText(),
Integer.parseInt(ClientLoad.ServerPort.getText()));
cin=new BufferedReader(new InputStreamReader(client.getInputStream()));
cout=new PrintWriter(client.getOutputStream(),true);
//cinObject=new ObjectInputStream();
cout.println(username);
cout.println(password);
String readLine1=cin.readLine();
if(readLine1.equals("ok"))
{
String readuserlist=cin.readLine();
String usernames[] = readuserlist.split(",");
userName.clear();
jlist.removeAll();
int i =1;
while(i<usernames.length)
{
userName.add(usernames[i]);
i++;
}
return true;
}
}catch(Exception e)
{
}
return false;
}
public void run()
{
String aline;
try{
while(chart!=null&&(aline=cin.readLine() )!=null)
{
if(aline.equals("exit")||aline.equals("stop"))
{
message.append("服务器已断开!重新登录!\n");
break;
}
else message.append("服务器说:"+aline+"\n");
Thread.sleep(200);
}
cin.close() ;
cout.close() ;
client.close() ;
}
catch(Exception e)
{ }
}
public void sendMessage(String mes)
{
cout.println(mes);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -