Java聊天室 IO流 Socket流 GUI界面 客户端PublicCompoent界面详解
效果图
- 发送按钮点击 发送公聊对象 界面显示内容 并保存至聊天记录
setButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setMessage=infortextArea.getText();
if(!Objects.equals(setMessage, "")){
User us = new User(MainView.ss,setMessage, MsgTypeConfig.PUBLIC_MSG,System.currentTimeMillis());
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(us.getTime());
Font fon = new Font("黑体", 1, 25);
infortextArea1.setFont(fon);
infortextArea1.append(MainView.ss+":"+"【"+timeStr+"】"+"\n");
infortextArea1.append(setMessage+"\n");
try {
send.writeObject(us);
String jname=MainView.ss+"MEto.txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff,jname);
file.createNewFile();
BufferedWriter bu = new BufferedWriter(new FileWriter(file,true));
bu.write("【我】"+MainView.ss + "向大家发来一条消息:" + "【"+timeStr+"】"+"\n");
bu.write(setMessage+"\n");
bu.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea.setText("");
}else {
JOptionPane.showMessageDialog(null, "不能发送空的消息!请输入消息!");
}
}
});
代码
package SanWa.UI;
import SanWa.Net.MsgTypeConfig;
import SanWa.Net.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Objects;
public class PublicCompoent extends Box {
private JPanel c1;
private JPanel c2;
String setMessage;
public static JTextArea infortextArea;
public static JTextArea infortextArea1;
public static ObjectOutputStream send= MainView.out;
public PublicCompoent() {
super(BoxLayout.Y_AXIS);
this.setBackground(new Color(208,208,208));
JLabel d1Label = new JLabel(" 公聊频道");//组装视图
d1Label.setForeground(Color.white);
JPanel btnPanel = new JPanel();
Color color = new Color(50,50,50);
d1Label.setFont(new java.awt.Font("楷体", 1, 30));
d1Label.setPreferredSize(new Dimension(770,39));
d1Label.setBounds(500,500,1500,600);
btnPanel.setBackground(color);
btnPanel.setMaximumSize(new Dimension(2000, 40));
btnPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
btnPanel.add(d1Label);
this.add(btnPanel);
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(850,330));
infortextArea1 = new JTextArea();
infortextArea1.setEditable(false);
infortextArea1.setBounds(0, 0, 850, 200);
infortextArea1.setBackground(new Color(208,208,208));
JScrollPane scrollPane21 = new JScrollPane();
scrollPane21.setBounds(0, 0, 850, 300);
scrollPane21.setViewportView(infortextArea1);
u1Box.add(scrollPane21);
this.add(u1Box);
JLabel pLabel = new JLabel("公共聊天频道,请输入要发送消息:");
pLabel.setFont(new Font("黑体", 1, 16));
c2=new JPanel();
c2.setLayout(new FlowLayout(FlowLayout.LEFT));
c2.setBackground(new Color(208,208,208));
c2.setPreferredSize(new Dimension(850,30));
JPanel btn1Panel = new JPanel();
Color color1 = new Color(208,208,208);
btn1Panel.setBackground(color1);
btn1Panel.add(pLabel);
c2.add(btn1Panel);
this.add(c2);
Box uBox = Box.createHorizontalBox();
uBox.setPreferredSize(new Dimension(850,180));
infortextArea = new JTextArea();
infortextArea.setFont(new java.awt.Font("楷体", 1, 16));
infortextArea.setBounds(0, 0, 850, 200);
infortextArea.setBackground(new Color(208,208,208));
JScrollPane scrollPane_1 = new JScrollPane();
uBox.add(scrollPane_1);
scrollPane_1.setViewportView(infortextArea);
this.add(uBox);
JPanel btnPanel1 = new JPanel();
Color color12 = new Color(208,208,208);
btnPanel1.setBackground(color12);
btnPanel1.setMaximumSize(new Dimension(1500,40));
btnPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton setButton = new JButton("发送");
setButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setMessage=infortextArea.getText();
if(!Objects.equals(setMessage, "")){
User us = new User(MainView.ss,setMessage, MsgTypeConfig.PUBLIC_MSG,System.currentTimeMillis());
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(us.getTime());
Font fon = new Font("黑体", 1, 25);
infortextArea1.setFont(fon);
infortextArea1.append(MainView.ss+":"+"【"+timeStr+"】"+"\n");
infortextArea1.append(setMessage+"\n");
try {
send.writeObject(us);
String jname=MainView.ss+"MEto.txt";
File ff = new File("ChatRoom\\JiLu");
File file = new File(ff,jname);
file.createNewFile();
BufferedWriter bu = new BufferedWriter(new FileWriter(file,true));
bu.write("【我】"+MainView.ss + "向大家发来一条消息:" + "【"+timeStr+"】"+"\n");
bu.write(setMessage+"\n");
bu.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea.setText("");
}else {
JOptionPane.showMessageDialog(null, "不能发送空的消息!请输入消息!");
}
}
});
JButton getButton1 = new JButton("取消");
btnPanel1.add(getButton1);
btnPanel1.add(setButton);
this.add(btnPanel1);
}
}