123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.FileOutputStream;
- import javax.swing.*;
- //////////*服务器窗口类*///////////////
- public class ServerFrame extends JFrame implements ActionListener {
- public JList list;
- /**
- *
- */
- private static final long serialVersionUID = -8936397327038098620L;
- // 服务器信息面板
- JPanel pnlServer, pnlServerInfo;
- JLabel lblStatus, lblNumber, lblMax, lblServerName, lblProtocol, lblIP, lblPort, lblLog;
- public JTextField txtStatus, txtNumber, txtMax, txtServerName, txtProtocol, txtIP, txtPort;
- JButton btnStop, btnSaveLog;
- public TextArea taLog;
- JTabbedPane tpServer;
- public TextArea taMessage;
- // 用户信息面板
- JPanel pnlUser;
- public JLabel lblMessage, lblUser, lblNotice, lblUserCount;
- JList lstUser;
- JScrollPane spUser;
- JTextField txtNotice;
- JButton btnSend, btnKick;
- public String serverMessage = "";
- public ServerFrame() {
- // 服务器窗口
- super("聊天服务器");
- setSize(550, 500);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setResizable(false);
- Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();// 在屏幕居中显示
- Dimension fra = this.getSize();
- if (fra.width > scr.width) {
- fra.width = scr.width;
- }
- if (fra.height > scr.height) {
- fra.height = scr.height;
- }
- this.setLocation((scr.width - fra.width) / 2, (scr.height - fra.height) / 2);
- // 服务器信息面板
- pnlServer = new JPanel();
- pnlServer.setLayout(null);
- pnlServer.setBackground(new Color(245, 245, 245));
- // 左边表格信息
- pnlServerInfo = new JPanel(new GridLayout(14, 1));
- pnlServerInfo.setBackground(Color.decode("#F5F5F5"));
- pnlServerInfo.setFont(new Font("Microsoft YaHei", 0, 12));
- pnlServerInfo.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""),
- BorderFactory.createEmptyBorder(1, 1, 1, 1)));
- // 表格列表
- // 第一行
- lblStatus = new JLabel("当前状态:");
- lblStatus.setForeground(Color.decode("#606060"));
- lblStatus.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第二行
- txtStatus = new JTextField(10);
- txtStatus.setBackground(Color.WHITE);
- txtStatus.setForeground(Color.decode("#606060"));
- txtStatus.setFont(new Font("Microsoft YaHei", 0, 12));
- txtStatus.setEditable(false);
- // 第三行
- lblNumber = new JLabel("当前在线人数:");
- lblNumber.setForeground(Color.decode("#606060"));
- lblNumber.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第四行
- txtNumber = new JTextField(10);
- txtNumber.setBackground(Color.WHITE);
- txtNumber.setForeground(Color.decode("#606060"));
- txtNumber.setFont(new Font("Microsoft YaHei", 0, 12));
- txtNumber.setEditable(false);
- // 第五行
- lblMax = new JLabel("最多在线人数:");
- lblMax.setForeground(Color.decode("#606060"));
- lblMax.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第六行
- txtMax = new JTextField("50 人", 10);
- txtMax.setBackground(Color.WHITE);
- txtMax.setForeground(Color.decode("#606060"));
- txtMax.setFont(new Font("Microsoft YaHei", 0, 12));
- txtMax.setEditable(false);
- // 第七行
- lblServerName = new JLabel("服务器名称:");
- lblServerName.setForeground(Color.decode("#606060"));
- lblServerName.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第八行
- txtServerName = new JTextField(10);
- txtServerName.setBackground(Color.WHITE);
- txtServerName.setForeground(Color.decode("#606060"));
- txtServerName.setFont(new Font("Microsoft YaHei", 0, 12));
- txtServerName.setEditable(false);
- // 第九行
- lblProtocol = new JLabel("访问协议:");
- lblProtocol.setForeground(Color.decode("#606060"));
- lblProtocol.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第十行
- txtProtocol = new JTextField("HTTP", 10);
- txtProtocol.setBackground(Color.WHITE);
- txtProtocol.setForeground(Color.decode("#606060"));
- txtProtocol.setFont(new Font("Microsoft YaHei", 0, 12));
- txtProtocol.setEditable(false);
- // 第十一行
- lblIP = new JLabel("服务器IP:");
- lblIP.setForeground(Color.decode("#606060"));
- lblIP.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第十二行
- txtIP = new JTextField(10);
- txtIP.setBackground(Color.WHITE);
- txtIP.setForeground(Color.decode("#606060"));
- txtIP.setFont(new Font("Microsoft YaHei", 0, 12));
- txtIP.setEditable(false);
- // 第十三行
- lblPort = new JLabel("服务器端口:");
- lblPort.setForeground(Color.decode("#606060"));
- lblPort.setFont(new Font("Microsoft YaHei", 0, 12));
- // 第十四行
- txtPort = new JTextField("8000", 10);
- txtPort.setBackground(Color.WHITE);
- txtPort.setForeground(Color.decode("#606060"));
- txtPort.setFont(new Font("Microsoft YaHei", 0, 12));
- txtPort.setEditable(false);
- // 按钮
- btnStop = new JButton("关闭服务器");
- btnStop.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- closeServer();
- }
- });
- btnStop.setBackground(Color.decode("#F5F5F5"));
- btnStop.setFont(new Font("Microsoft YaHei", 0, 12));
- btnStop.setForeground(Color.decode("606060"));
- // 日志
- lblLog = new JLabel("服务器日志");
- lblLog.setFont(new Font("Microsoft YaHei", 0, 12));
- lblLog.setForeground(Color.decode("#606060"));
- taLog = new TextArea(20, 50);
- taLog.setFont(new Font("Microsoft YaHei", 0, 12));
- btnSaveLog = new JButton("保存日志");
- btnSaveLog.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- saveLog();
- }
- });
- btnSaveLog.setBackground(Color.decode("#F5F5F5"));
- btnSaveLog.setFont(new Font("Microsoft YaHei", 0, 12));
- btnSaveLog.setForeground(Color.decode("606060"));
- pnlServerInfo.add(lblStatus);
- pnlServerInfo.add(txtStatus);
- pnlServerInfo.add(lblNumber);
- pnlServerInfo.add(txtNumber);
- pnlServerInfo.add(lblMax);
- pnlServerInfo.add(txtMax);
- pnlServerInfo.add(lblServerName);
- pnlServerInfo.add(txtServerName);
- pnlServerInfo.add(lblProtocol);
- pnlServerInfo.add(txtProtocol);
- pnlServerInfo.add(lblIP);
- pnlServerInfo.add(txtIP);
- pnlServerInfo.add(lblPort);
- pnlServerInfo.add(txtPort);
- pnlServerInfo.setBounds(5, 5, 100, 400);
- lblLog.setBounds(110, 5, 100, 30);
- taLog.setBounds(110, 35, 400, 370);
- btnStop.setBounds(150, 410, 120, 30);
- btnSaveLog.setBounds(270, 410, 120, 30);
- pnlServer.add(pnlServerInfo);
- pnlServer.add(lblLog);
- pnlServer.add(taLog);
- pnlServer.add(btnStop);
- pnlServer.add(btnSaveLog);
- // 用户面板
- pnlUser = new JPanel();
- pnlUser.setLayout(null);
- pnlUser.setBackground(new Color(245, 245, 245));
- pnlUser.setFont(new Font("Microsoft YaHei", 0, 12));
- lblMessage = new JLabel("用户消息");
- lblMessage.setFont(new Font("Microsoft YaHei", 0, 12));
- lblMessage.setForeground(Color.decode("#606060"));
- taMessage = new TextArea(20, 20);
- taMessage.setFont(new Font("Microsoft YaHei", 0, 12));
- lblNotice = new JLabel("通知:");
- lblNotice.setFont(new Font("Microsoft YaHei", 0, 12));
- lblNotice.setForeground(Color.decode("#606060"));
- txtNotice = new JTextField(20);
- txtNotice.setFont(new Font("Microsoft YaHei", 0, 12));
- btnSend = new JButton("发送");
- btnSend.setBackground(Color.decode("#f5f5f5"));
- btnSend.setFont(new Font("Microsoft YaHei", 0, 12));
- btnSend.setForeground(Color.decode("#606060"));
- btnSend.setEnabled(true);
- btnSend.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- serverMessage();
- }
- });
- lblUserCount = new JLabel("在线总人数 0 人");
- lblUserCount.setFont(new Font("Microsoft YaHei", 0, 12));
- lblUserCount.setForeground(Color.decode("#606060"));
- lblUser = new JLabel("在线用户列表");
- lblUser.setFont(new Font("Microsoft YaHei", 0, 12));
- lblUser.setForeground(Color.decode("#606060"));
- lstUser = new JList();
- lstUser.setFont(new Font("Microsoft YaHei", 0, 12));
- lstUser.setVisibleRowCount(17);
- lstUser.setFixedCellWidth(180);
- lstUser.setFixedCellHeight(18);
- spUser = new JScrollPane();
- spUser.setBackground(Color.decode("#f5f5f5"));
- spUser.setFont(new Font("Microsoft YaHei", 0, 12));
- spUser.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- spUser.getViewport().setView(lstUser);
- lblMessage.setBounds(5, 5, 100, 25);
- taMessage.setBounds(5, 35, 300, 360);
- lblUser.setBounds(310, 5, 100, 25);
- spUser.setBounds(310, 35, 220, 360);
- lblNotice.setBounds(5, 410, 40, 25);
- txtNotice.setBounds(50, 410, 160, 25);
- btnSend.setBounds(210, 410, 80, 25);
- lblUserCount.setBounds(320, 410, 100, 25);
- pnlUser.add(lblMessage);
- pnlUser.add(taMessage);
- pnlUser.add(lblUser);
- pnlUser.add(spUser);
- list = new JList<>();
- list.setListData(new String[] { "" });
- spUser.setViewportView(list);
- pnlUser.add(lblNotice);
- pnlUser.add(txtNotice);
- pnlUser.add(btnSend);
- pnlUser.add(lblUserCount);
- // 主标签面板
- tpServer = new JTabbedPane(JTabbedPane.LEFT);
- tpServer.setBackground(Color.decode("#f5f5f5"));
- tpServer.setFont(new Font("Microsoft YaHei", 0, 12));
- tpServer.add("服务器管理", pnlServer);
- tpServer.add("用户信息管理", pnlUser);
- this.getContentPane().add(tpServer);
- setVisible(true);
- }
- protected void serverMessage() {
- this.serverMessage = txtNotice.getText();
- txtNotice.setText("");
- }
- protected void closeServer() {
- this.dispose();
- }
- protected void saveLog() {
- try {
- FileOutputStream fileoutput = new FileOutputStream("log.txt", true);
- String temp = taMessage.getText();
- fileoutput.write(temp.getBytes());
- fileoutput.close();
- JOptionPane.showMessageDialog(null, "记录保存在log.txt");
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- private void log(String string) {
- String newta = taMessage.getText();
- newta += ("\n" + string);
- taMessage.setText(newta);
- }
- public void actionPerformed(ActionEvent evt) {
- }
- public static void main(String args[]) {
- new ServerFrame();
- }
- }
|