📄 chatroom.java
字号:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatRoom extends JFrame implements ActionListener, Runnable {
//---------主标签页面的变量需求---------------
JTabbedPane tpServer;
// 用于将窗口用于定位
Dimension scrnsize;
Toolkit toolkit = Toolkit.getDefaultToolkit();
static JFrame frmChat;
//---------公共聊天室的变量需求---------------
private JComboBox daXiaoComboBox;
private JComboBox yangShiComboBox;
private JComboBox zitiComboBox;
JPanel pnlChat;
JButton btnCls, btnExit, btnSend, btnClear, btnSave, btnTimer;
JLabel lblUserList, lblUserMessage;
JLabel lblUserTotal, lblCount, lblBack;
JLabel red ,blue ,green ;
JTextField txtMessage ;
TextArea taUserMessage;
List lstUserList;
String strServerIp, strLoginName;
Thread thread;
Message messobj = null;
String serverMessage = "";
final JSlider blueSlider = new JSlider();
final JSlider redSlider = new JSlider();
final JSlider greenSlider = new JSlider();
final JLabel hanziLabel = new JLabel();
//--------------私人信息及聊天管理界面---------------
JPanel userChat,userChat1;
JButton queryInformation, queryAllLog ,queryPslLog,changePassword;
JLabel secretChat,secretChatToUser;
JLabel friendOfUser,lblLogo;
JButton secret;
JTextField secretChatToUserOfName ;
public JList friend;
JList lstFriend;
JScrollPane spUser;
// 构造方法
public ChatRoom(String name, String ip) {
strServerIp = ip;
strLoginName = name;
//------------主标签页面--------------
frmChat = new JFrame("聊天室" + "[用户:" + name + "]");
frmChat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//-----------------公共聊天室界面--------------------
Font fntDisp1 = new Font("宋体", Font.PLAIN, 12);
pnlChat = new JPanel();
btnCls = new JButton("清屏(C)");
btnExit = new JButton("退出(X)");
btnSend = new JButton("发送(N)");
btnSave = new JButton("保存(S)");
btnTimer = new JButton("时钟(T)");
lblUserList = new JLabel("【在线用户列表】");
lblUserMessage = new JLabel("【聊天信息】");
lblUserTotal = new JLabel("在线人数:");
lblCount = new JLabel("0");
red = new JLabel("红");
blue = new JLabel("蓝");
green = new JLabel("绿");
lstUserList = new java.awt.List();
txtMessage = new JTextField(170);
taUserMessage = new TextArea("", 300, 200,
TextArea.SCROLLBARS_VERTICAL_ONLY);// 只能向下滚动
taUserMessage.setForeground(new Color(0, 0, 0));
taUserMessage.setEditable(false); // 不可写入
/***********************************************************************
* 该布局采用手动布局 setBounds设置组件位置 * setFont设置字体、字型、字号 * setForeground设置文字的颜色 *
* setBackground设置背景色 * setOpaque将背景设置为透明
*/
pnlChat.setLayout(null);
pnlChat.setBackground(new Color(125,100,175));
btnTimer.setBounds(500, 300, 80, 25);
btnSave.setBounds(500, 350, 80, 25);
btnCls.setBounds(400, 400, 80, 25);//500, 300, 80, 25
btnExit.setBounds(500, 400, 80, 25);
btnSend.setBounds(230, 400, 80, 25);
lblUserList.setBounds(5, 0, 120, 40);
lblUserTotal.setBounds(130, 0, 60, 40);
lblCount.setBounds(190, 0, 60, 40);
lblUserMessage.setBounds(225, 0, 180, 40);
red.setBounds(0, 380, 180, 10);
blue.setBounds(0, 365, 180, 10);
green.setBounds(0, 395, 180, 10);
// lblUserTotal.setBounds(10,340,100,40);
// lblCount.setBounds(73,340,100,40);
lstUserList.setBounds(5, 40, 210, 255);
taUserMessage.setBounds(225, 40, 360, 255);
txtMessage.setBounds(230, 300, 250, 80);
btnTimer.setFont(fntDisp1);
btnCls.setFont(fntDisp1);
btnExit.setFont(fntDisp1);
btnSend.setFont(fntDisp1);
btnSave.setFont(fntDisp1);
lblUserList.setFont(fntDisp1);
lblUserMessage.setFont(fntDisp1);
lblUserTotal.setFont(fntDisp1);
lblCount.setFont(fntDisp1);
lblUserList.setForeground(Color.YELLOW);
lblUserMessage.setForeground(Color.YELLOW);
lblUserTotal.setForeground(Color.YELLOW);
lblCount.setForeground(Color.YELLOW);
lstUserList.setBackground(Color.white);
taUserMessage.setBackground(Color.white);
btnTimer.setBackground(Color.ORANGE);
btnCls.setBackground(Color.ORANGE);
btnExit.setBackground(Color.ORANGE);
btnSend.setBackground(Color.ORANGE);
btnSave.setBackground(Color.ORANGE);
pnlChat.add(btnTimer);
pnlChat.add(btnCls);
pnlChat.add(btnExit);
pnlChat.add(btnSend);
pnlChat.add(btnSave);
pnlChat.add(lblUserList);
pnlChat.add(lblUserMessage);
pnlChat.add(red);
pnlChat.add(green);
pnlChat.add(blue);
pnlChat.add(lblUserTotal);
pnlChat.add(lblCount);
pnlChat.add(lstUserList);
pnlChat.add(taUserMessage);
pnlChat.add(txtMessage);
btnTimer.addActionListener(this);
btnCls.addActionListener(this);
btnExit.addActionListener(this);
btnSend.addActionListener(this);
btnSave.addActionListener(this);
lstUserList.addActionListener(this);
txtMessage.addActionListener(this);
zitiComboBox = new JComboBox();
zitiComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
freshFont();
}
});
zitiComboBox.setModel(new DefaultComboBoxModel(new String[] { "宋体",
"黑体" }));
zitiComboBox.setBounds(0, 300, 64, 23);
pnlChat.add(zitiComboBox);
yangShiComboBox = new JComboBox();
yangShiComboBox.setModel(new DefaultComboBoxModel(new String[] { "正规",
"粗体", "斜体", "粗斜" }));
yangShiComboBox.setBounds(85, 300, 52, 23);
yangShiComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
freshFont();
}
});
pnlChat.add(yangShiComboBox);
daXiaoComboBox = new JComboBox();
daXiaoComboBox.setModel(new DefaultComboBoxModel(new String[] { "12",
"14", "16", "18", "20" }));
daXiaoComboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
freshFont();
}
});
daXiaoComboBox.setBounds(165, 300, 40, 23);
pnlChat.add(daXiaoComboBox);
blueSlider.setMaximum(255);
blueSlider.setValue(128);
blueSlider.setMinimum(0);
blueSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
// TODO 自动生成方法存根
freshColor();
}
});
// slider.setModel(null);
blueSlider.setBounds(15, 365, 180, 10);
pnlChat.add(blueSlider);
redSlider.setMaximum(255);
redSlider.setValue(128);
redSlider.setMinimum(0);
redSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
// TODO 自动生成方法存根
freshColor();
}
});
redSlider.setBounds(15, 380, 180, 10);
pnlChat.add(redSlider);
greenSlider.setMaximum(255);
greenSlider.setValue(128);
greenSlider.setMinimum(0);
greenSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
// TODO 自动生成方法存根
freshColor();
}
});
greenSlider.setBounds(15, 395, 180, 10);
pnlChat.add(greenSlider);
hanziLabel.setForeground(new Color(128, 128, 128));
hanziLabel.setHorizontalAlignment(SwingConstants.CENTER);
hanziLabel.setFont(new Font("宋体", Font.BOLD, 18));
hanziLabel.setBackground(Color.ORANGE);
hanziLabel.setBounds(273, 360, 57, 50);
pnlChat.add(hanziLabel);
//---------------私人信息管理以及聊天界面-----------------
userChat = new JPanel();
userChat.setLayout(null);
userChat.setBackground(new Color(125,100,175));
userChat1 = new JPanel(new GridLayout(8, 1));
userChat1 .setBackground(new Color(125,100,175));
userChat1 .setFont(new Font("宋体", 0, 12));
userChat1 .setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(""), BorderFactory
.createEmptyBorder(1, 1, 1, 1)));
queryInformation = new JButton("查询基本信息");
queryInformation.setForeground(Color.BLACK);
queryInformation.setFont(new Font("宋体", 0, 12));
queryAllLog = new JButton("查看公共聊天室记录");
queryAllLog.setForeground(Color.BLACK);
queryAllLog.setFont(new Font("宋体", 0, 12));
queryPslLog = new JButton("查看私人聊天室记录");
queryPslLog.setForeground(Color.BLACK);
queryPslLog.setFont(new Font("宋体", 0, 12));
changePassword = new JButton("修改密码");
changePassword.setForeground(Color.BLACK);
changePassword.setFont(new Font("宋体", 0, 12));// JLabel secretChat,secretChatToUser,secret; JTextField secretChatToUserOfName ;
secretChat = new JLabel("私人聊天");
secretChat.setForeground(Color.YELLOW);
secretChat.setFont(new Font("宋体", 0, 35));
secretChatToUser = new JLabel("聊天对象:");
secretChatToUser.setForeground(Color.BLACK);
secretChatToUser.setFont(new Font("宋体", 0, 12));
secretChatToUserOfName = new JTextField(10);
secretChatToUserOfName.setBackground(Color.decode("#d6f4f2"));
secretChatToUserOfName.setFont(new Font("宋体", 0, 12));
secret = new JButton("私聊");
secret.setForeground(Color.BLACK);
secret.setFont(new Font("宋体", 0, 12));
queryInformation.addActionListener(this);
queryAllLog.addActionListener(this);
queryPslLog.addActionListener(this);
changePassword.addActionListener(this);
secret.addActionListener(this);
frmChat.addWindowListener(new Windowclose());
queryInformation.setBounds(100,100, 100,100);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -