Java聊天室 IO流 Socket流 GUI界面 客户端UserManageCompoent界面详解
效果图
-
获取按钮点击 发送获取对象 界面显示在线列表
getButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST,System.currentTimeMillis()); try { send.writeObject(us); } catch (IOException ioException) { ioException.printStackTrace(); } infortextArea1.setText(""); } });
代码
package SanWa.UI;
import SanWa.Net.User;
import SanWa.Net.MsgTypeConfig;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class UserManageCompoent extends Box {
public static JTextArea infortextArea1;
public static ObjectOutputStream send= MainView.out;
public UserManageCompoent() {
//垂直布局
super(BoxLayout.Y_AXIS);
//组装视图
Box u1Box = Box.createHorizontalBox();
u1Box.setPreferredSize(new Dimension(850, 560));
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);
JPanel btnPanel = new JPanel();
Color color = new Color(208,208,208);
btnPanel.setBackground(new Color(50,50,50));
btnPanel.setMaximumSize(new Dimension(1500, 80));
btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JLabel d11Label = new JLabel(" 当前在线成员列表 ");
d11Label.setBackground(new Color(50,50,50));
d11Label.setForeground(Color.white);
d11Label.setFont(new java.awt.Font("楷体", 1, 35));
JButton getButton = new JButton("获取");
getButton.setFont(new java.awt.Font("楷体", 1, 20));
getButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
User us = new User(MainView.ss,null,null, MsgTypeConfig.ONLINE_LIST,System.currentTimeMillis());
try {
send.writeObject(us);
} catch (IOException ioException) {
ioException.printStackTrace();
}
infortextArea1.setText("");
}
});
btnPanel.add(d11Label);
btnPanel.add(getButton);
this.add(btnPanel);
Box u5Box = Box.createHorizontalBox(); u5Box.setBackground(new Color(208,208,208));
JScrollPane scrollPane_5 = new JScrollPane();
scrollPane_5.setBackground(new Color(208,208,208));
Box btn1Panel = Box.createHorizontalBox();
btn1Panel.setPreferredSize(new Dimension(600, 60));
btn1Panel.setBackground(new Color(208,208,208));
JLabel d1Label = new JLabel("【编号】 ");
JLabel d2Label = new JLabel("【编号】 【姓名】 ");
JLabel d3Label = new JLabel("【编号】 【姓名】 【IP 地 址】 ");
JLabel d4Label = new JLabel("【编号】 【姓名】 【IP 地 址】 【端口号】 ");//组装视图
JLabel d5Label = new JLabel("【编号】 【姓名】 【IP 地 址】 【端口号】 【本地端口号】 ");//组装视图
JLabel d6Label = new JLabel("【编号】 【姓名】 【IP 地 址】 【端口号】 【本地端口号】 【在线状态】");//组装视图
Color color1 = new Color(208,208,208);
d1Label.setFont(new java.awt.Font("楷体", 1, 17));
d2Label.setFont(new java.awt.Font("楷体", 1, 17));
d3Label.setFont(new java.awt.Font("楷体", 1, 17));
d4Label.setFont(new java.awt.Font("楷体", 1, 17));
d5Label.setFont(new java.awt.Font("楷体", 1, 17));
d6Label.setFont(new java.awt.Font("楷体", 1, 17));
btn1Panel.setBackground(color1);
btn1Panel.setMaximumSize(new Dimension(600, 40));
btn1Panel.add(d1Label);
btn1Panel.add(d2Label);
btn1Panel.add(d3Label);
btn1Panel.add(d4Label);
btn1Panel.add(d5Label);
btn1Panel.add(d6Label);
JTextArea infortext = new JTextArea();
infortext.setEditable(false);
infortext.setText("【编号】 【姓名】 【IP地址】 【端口号】 【本地端口号】 【在线状态】");
infortext.setFont(new java.awt.Font("楷体", 1, 20));
infortext.setBackground(color);
infortext.add(btn1Panel);
scrollPane_5.setViewportView(infortext);
u5Box.add(scrollPane_5);
this.add(u5Box);
this.add(u1Box);
}
}