📄 chat_client.java
字号:
package chat1;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Chat_client extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private ObjectInputStream in;
private ObjectOutputStream out;
private String message = "";
// private String Localhost;
private Socket toclient;
//private String ss[]={"宋体","楷体","华文行楷","新宋体"};
JMenuBar jmb1;
JToolBar jtb1;
JToolBar jtb2;
JButton jm1;
JButton jm2;
JButton jm3;
JButton jm4;
JButton connect;
JButton selfout;
JButton selcolor;
JButton back1;
JButton back2;
JButton selface;
JButton selbg;
JButton selsound;
JPanel jp1;
JPanel jp2;
JList jl;
JTextArea jta1;
JTextField jtf;
Container con;
JLabel label;
JSeparator js1;
JSeparator js2;
Color color;
BufferedImage bufimage;
Icon bg1;
Icon bg2;
Icon bg3;
Icon bg4;
Dimension size;
Font font;
// JComboBox jcb;
public Chat_client()
{
con = this.getContentPane();
jp1 = new JPanel();
jp2 = new JPanel();
jp1.setLayout(new BorderLayout());
jp2.setLayout(new BorderLayout());
jta1=new JTextArea("");
jtf = new JTextField("Please input:");
jta1.setBackground(Color.LIGHT_GRAY);
jtf.setBackground(Color.LIGHT_GRAY);
jta1.setEditable(false);
jta1.setEnabled(true);
jtf.setEditable(true);
jtf.addActionListener(this);
jmb1 = new JMenuBar();
jmb1.setBackground(Color.pink);
jtb1 = new JToolBar();
jtb1.setBackground(Color.pink);
js1 = new JSeparator();
js2 = new JSeparator();
jl = new JList();
label = new JLabel("制作人:Jimmy");
label.setForeground(Color.BLUE);
jtb2 = new JToolBar();
jtb2.setBackground(Color.pink);
jtb2.add(label);
jp2.add( new JScrollPane(jtf));
jp2.add(jtb1,"North");
jp2.add(jtb2,"South");
jm1 = new JButton("在线");
jm1.setBackground(Color.orange);
jmb1.add(jm1);
jm2 = new JButton("离线");
jm2.setBackground(Color.orange);
jmb1.add(jm2);
jm3 = new JButton("隐身");
jm3.setBackground(Color.orange);
jmb1.add(jm3);
jm4 = new JButton("帮助");
jm4.setBackground(Color.orange);
jm4.addActionListener(this);
jmb1.add(jm4);
// jcb = new JComboBox(ss);
connect = new JButton("连接服务器");
connect.setBackground(Color.yellow);
connect.addActionListener(this);
selface = new JButton("表情选择");
selface.setBackground(Color.orange);
selface.addActionListener(this);
selbg = new JButton("情景选择");
selbg.setBackground(Color.orange);
selbg.addActionListener(this);
selfout = new JButton("字体选择");
selfout.setBackground(Color.orange);
selfout.addActionListener(this);
selcolor = new JButton("字体颜色");
selcolor.setBackground(Color.orange);
selcolor.addActionListener(this);
selsound = new JButton("选择音乐");
selsound.addActionListener(this);
selsound.setBackground(Color.orange);
jtb1.add(selcolor);
jtb1.add(jl);
jtb1.add(selfout);
// jtb1.add(jcb);
jtb1.add(selface);
jtb1.add(selbg);
jtb1.add(selsound);
jtb1.add(connect,"East");
bg1 = new ImageIcon("micky.jpg");
bg2 = new ImageIcon("23.jpg");
back1 = new JButton();
back2 = new JButton();
back1.setIcon(bg1);
back2.setIcon(bg2);
back1.setBackground(Color.pink);
back2.setBackground(Color.pink);
jp1.add(back1,"North");
jp1.add(js2);
jp1.add(back2,"South");
con.setLayout(new BorderLayout());
con.add(jmb1,"North");
//con.add(jta1);
con.add(jp2,"South");
con.add(jp1,"East");
con.add(new JScrollPane(jta1));
size = this.getToolkit().getScreenSize();
this.setJMenuBar(jmb1);
this.setIconImage(getToolkit().getImage("qq.jpg"));
this.setBackground(Color.orange);
this.setSize(size.width - 300, size.height /2 +27);
this.setResizable(false);
this.setLocation(100,220);
this.setTitle("Welcome to the chat room !");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand().equals("字体颜色") ){
color = JColorChooser.showDialog(null, "请选择颜色", null);
}
if(evt.getActionCommand().equals("字体选择")){
JOptionPane.showMessageDialog(null,"Sorry, you can't set the font that time!");
}
if(evt.getActionCommand().equals("情景选择")){
JOptionPane.showMessageDialog(null, "Sorry,you should use the tolerant background !");
}
if(evt.getActionCommand().equals("选择音乐")){
JOptionPane.showMessageDialog(null, "Sorry,the function is down");
}
if(evt.getActionCommand().equals("表情选择")){
JOptionPane.showMessageDialog(null, "Sorry ");
}
if(evt.getSource()==jm4){
JOptionPane.showMessageDialog(null, "这是一个基于局域网的聊天软件," +"\n"+
"所以请在同一局域网中运行。" +"\n"+
"如果出现不能连接的情况,请关闭Client端," +"\n"+
"在打开server端的情况下运行客户端!" +"\n"+
"如果还不能连接,请查看源程序!");
}
if(evt.getSource()==jtf){
if(jtf.getText().trim().length()>0)
{
String st=jtf.getText();
font = new Font(st,100 ,100);
//jta1.append("\n" + st);
jta1.setForeground(color);
// jtf.setText("");
jtf.setForeground(color);
}
sendMsg(jtf.getText());
}
if(evt.getSource()==connect){
connectClient();
}
}
public void connectClient(){
try{
connectServer();
getStreams();
processConnection();
closeConnection();
}
catch(EOFException eofException){
System.out.println("Server terminated connection");
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
private void connectServer() throws IOException{
jta1.setText("连接中......\n");
toclient = new Socket(InetAddress.getByName("127.0.0.1"),6789);
jta1.append("连接至:"+toclient.getInetAddress().getHostName());
}
private void getStreams() throws IOException{
out = new ObjectOutputStream(toclient.getOutputStream());
out.flush();
in = new ObjectInputStream(toclient.getInputStream());
jta1.append("\nPlease input ...");
}
private void processConnection() throws IOException{
jtf.setEnabled(true);
do{
try{
message = (String)in.readObject();
jta1.append("\n"+message);
jta1.setCaretPosition(jta1.getText().length());
}
catch(ClassNotFoundException classnotfoundexcetion){
jta1.append("\nUnkonwn object type received");
}
}
while(!message.equals("Server:TERMINATE"));
}
private void closeConnection() throws IOException{
jta1.append("\n关闭连接") ;
out.close();
in.close();
toclient.close();
}
private void sendMsg(String message){
try{
out.writeObject("Client: \n"+message);
out.flush();
jta1.append("\nClient: \n"+message);
jtf.setText("");
}
catch(IOException ioException){
jta1.append("\nThe connection is cut!");
}
}
public static void main(String[] args)
{
Chat_client cc=new Chat_client();
cc.connectClient();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -