ServerFrame.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.FileOutputStream;
  5. import javax.swing.*;
  6. //////////*服务器窗口类*///////////////
  7. public class ServerFrame extends JFrame implements ActionListener {
  8. public JList list;
  9. /**
  10. *
  11. */
  12. private static final long serialVersionUID = -8936397327038098620L;
  13. // 服务器信息面板
  14. JPanel pnlServer, pnlServerInfo;
  15. JLabel lblStatus, lblNumber, lblMax, lblServerName, lblProtocol, lblIP, lblPort, lblLog;
  16. public JTextField txtStatus, txtNumber, txtMax, txtServerName, txtProtocol, txtIP, txtPort;
  17. JButton btnStop, btnSaveLog;
  18. public TextArea taLog;
  19. JTabbedPane tpServer;
  20. public TextArea taMessage;
  21. // 用户信息面板
  22. JPanel pnlUser;
  23. public JLabel lblMessage, lblUser, lblNotice, lblUserCount;
  24. JList lstUser;
  25. JScrollPane spUser;
  26. JTextField txtNotice;
  27. JButton btnSend, btnKick;
  28. public String serverMessage = "";
  29. public ServerFrame() {
  30. // 服务器窗口
  31. super("聊天服务器");
  32. setSize(550, 500);
  33. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34. setResizable(false);
  35. Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();// 在屏幕居中显示
  36. Dimension fra = this.getSize();
  37. if (fra.width > scr.width) {
  38. fra.width = scr.width;
  39. }
  40. if (fra.height > scr.height) {
  41. fra.height = scr.height;
  42. }
  43. this.setLocation((scr.width - fra.width) / 2, (scr.height - fra.height) / 2);
  44. // 服务器信息面板
  45. pnlServer = new JPanel();
  46. pnlServer.setLayout(null);
  47. pnlServer.setBackground(new Color(245, 245, 245));
  48. // 左边表格信息
  49. pnlServerInfo = new JPanel(new GridLayout(14, 1));
  50. pnlServerInfo.setBackground(Color.decode("#F5F5F5"));
  51. pnlServerInfo.setFont(new Font("Microsoft YaHei", 0, 12));
  52. pnlServerInfo.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""),
  53. BorderFactory.createEmptyBorder(1, 1, 1, 1)));
  54. // 表格列表
  55. // 第一行
  56. lblStatus = new JLabel("当前状态:");
  57. lblStatus.setForeground(Color.decode("#606060"));
  58. lblStatus.setFont(new Font("Microsoft YaHei", 0, 12));
  59. // 第二行
  60. txtStatus = new JTextField(10);
  61. txtStatus.setBackground(Color.WHITE);
  62. txtStatus.setForeground(Color.decode("#606060"));
  63. txtStatus.setFont(new Font("Microsoft YaHei", 0, 12));
  64. txtStatus.setEditable(false);
  65. // 第三行
  66. lblNumber = new JLabel("当前在线人数:");
  67. lblNumber.setForeground(Color.decode("#606060"));
  68. lblNumber.setFont(new Font("Microsoft YaHei", 0, 12));
  69. // 第四行
  70. txtNumber = new JTextField(10);
  71. txtNumber.setBackground(Color.WHITE);
  72. txtNumber.setForeground(Color.decode("#606060"));
  73. txtNumber.setFont(new Font("Microsoft YaHei", 0, 12));
  74. txtNumber.setEditable(false);
  75. // 第五行
  76. lblMax = new JLabel("最多在线人数:");
  77. lblMax.setForeground(Color.decode("#606060"));
  78. lblMax.setFont(new Font("Microsoft YaHei", 0, 12));
  79. // 第六行
  80. txtMax = new JTextField("50 人", 10);
  81. txtMax.setBackground(Color.WHITE);
  82. txtMax.setForeground(Color.decode("#606060"));
  83. txtMax.setFont(new Font("Microsoft YaHei", 0, 12));
  84. txtMax.setEditable(false);
  85. // 第七行
  86. lblServerName = new JLabel("服务器名称:");
  87. lblServerName.setForeground(Color.decode("#606060"));
  88. lblServerName.setFont(new Font("Microsoft YaHei", 0, 12));
  89. // 第八行
  90. txtServerName = new JTextField(10);
  91. txtServerName.setBackground(Color.WHITE);
  92. txtServerName.setForeground(Color.decode("#606060"));
  93. txtServerName.setFont(new Font("Microsoft YaHei", 0, 12));
  94. txtServerName.setEditable(false);
  95. // 第九行
  96. lblProtocol = new JLabel("访问协议:");
  97. lblProtocol.setForeground(Color.decode("#606060"));
  98. lblProtocol.setFont(new Font("Microsoft YaHei", 0, 12));
  99. // 第十行
  100. txtProtocol = new JTextField("HTTP", 10);
  101. txtProtocol.setBackground(Color.WHITE);
  102. txtProtocol.setForeground(Color.decode("#606060"));
  103. txtProtocol.setFont(new Font("Microsoft YaHei", 0, 12));
  104. txtProtocol.setEditable(false);
  105. // 第十一行
  106. lblIP = new JLabel("服务器IP:");
  107. lblIP.setForeground(Color.decode("#606060"));
  108. lblIP.setFont(new Font("Microsoft YaHei", 0, 12));
  109. // 第十二行
  110. txtIP = new JTextField(10);
  111. txtIP.setBackground(Color.WHITE);
  112. txtIP.setForeground(Color.decode("#606060"));
  113. txtIP.setFont(new Font("Microsoft YaHei", 0, 12));
  114. txtIP.setEditable(false);
  115. // 第十三行
  116. lblPort = new JLabel("服务器端口:");
  117. lblPort.setForeground(Color.decode("#606060"));
  118. lblPort.setFont(new Font("Microsoft YaHei", 0, 12));
  119. // 第十四行
  120. txtPort = new JTextField("8000", 10);
  121. txtPort.setBackground(Color.WHITE);
  122. txtPort.setForeground(Color.decode("#606060"));
  123. txtPort.setFont(new Font("Microsoft YaHei", 0, 12));
  124. txtPort.setEditable(false);
  125. // 按钮
  126. btnStop = new JButton("关闭服务器");
  127. btnStop.addActionListener(new ActionListener() {
  128. @Override
  129. public void actionPerformed(ActionEvent e) {
  130. closeServer();
  131. }
  132. });
  133. btnStop.setBackground(Color.decode("#F5F5F5"));
  134. btnStop.setFont(new Font("Microsoft YaHei", 0, 12));
  135. btnStop.setForeground(Color.decode("606060"));
  136. // 日志
  137. lblLog = new JLabel("服务器日志");
  138. lblLog.setFont(new Font("Microsoft YaHei", 0, 12));
  139. lblLog.setForeground(Color.decode("#606060"));
  140. taLog = new TextArea(20, 50);
  141. taLog.setFont(new Font("Microsoft YaHei", 0, 12));
  142. btnSaveLog = new JButton("保存日志");
  143. btnSaveLog.addActionListener(new ActionListener() {
  144. @Override
  145. public void actionPerformed(ActionEvent e) {
  146. saveLog();
  147. }
  148. });
  149. btnSaveLog.setBackground(Color.decode("#F5F5F5"));
  150. btnSaveLog.setFont(new Font("Microsoft YaHei", 0, 12));
  151. btnSaveLog.setForeground(Color.decode("606060"));
  152. pnlServerInfo.add(lblStatus);
  153. pnlServerInfo.add(txtStatus);
  154. pnlServerInfo.add(lblNumber);
  155. pnlServerInfo.add(txtNumber);
  156. pnlServerInfo.add(lblMax);
  157. pnlServerInfo.add(txtMax);
  158. pnlServerInfo.add(lblServerName);
  159. pnlServerInfo.add(txtServerName);
  160. pnlServerInfo.add(lblProtocol);
  161. pnlServerInfo.add(txtProtocol);
  162. pnlServerInfo.add(lblIP);
  163. pnlServerInfo.add(txtIP);
  164. pnlServerInfo.add(lblPort);
  165. pnlServerInfo.add(txtPort);
  166. pnlServerInfo.setBounds(5, 5, 100, 400);
  167. lblLog.setBounds(110, 5, 100, 30);
  168. taLog.setBounds(110, 35, 400, 370);
  169. btnStop.setBounds(150, 410, 120, 30);
  170. btnSaveLog.setBounds(270, 410, 120, 30);
  171. pnlServer.add(pnlServerInfo);
  172. pnlServer.add(lblLog);
  173. pnlServer.add(taLog);
  174. pnlServer.add(btnStop);
  175. pnlServer.add(btnSaveLog);
  176. // 用户面板
  177. pnlUser = new JPanel();
  178. pnlUser.setLayout(null);
  179. pnlUser.setBackground(new Color(245, 245, 245));
  180. pnlUser.setFont(new Font("Microsoft YaHei", 0, 12));
  181. lblMessage = new JLabel("用户消息");
  182. lblMessage.setFont(new Font("Microsoft YaHei", 0, 12));
  183. lblMessage.setForeground(Color.decode("#606060"));
  184. taMessage = new TextArea(20, 20);
  185. taMessage.setFont(new Font("Microsoft YaHei", 0, 12));
  186. lblNotice = new JLabel("通知:");
  187. lblNotice.setFont(new Font("Microsoft YaHei", 0, 12));
  188. lblNotice.setForeground(Color.decode("#606060"));
  189. txtNotice = new JTextField(20);
  190. txtNotice.setFont(new Font("Microsoft YaHei", 0, 12));
  191. btnSend = new JButton("发送");
  192. btnSend.setBackground(Color.decode("#f5f5f5"));
  193. btnSend.setFont(new Font("Microsoft YaHei", 0, 12));
  194. btnSend.setForeground(Color.decode("#606060"));
  195. btnSend.setEnabled(true);
  196. btnSend.addActionListener(new ActionListener() {
  197. public void actionPerformed(ActionEvent arg0) {
  198. serverMessage();
  199. }
  200. });
  201. lblUserCount = new JLabel("在线总人数 0 人");
  202. lblUserCount.setFont(new Font("Microsoft YaHei", 0, 12));
  203. lblUserCount.setForeground(Color.decode("#606060"));
  204. lblUser = new JLabel("在线用户列表");
  205. lblUser.setFont(new Font("Microsoft YaHei", 0, 12));
  206. lblUser.setForeground(Color.decode("#606060"));
  207. lstUser = new JList();
  208. lstUser.setFont(new Font("Microsoft YaHei", 0, 12));
  209. lstUser.setVisibleRowCount(17);
  210. lstUser.setFixedCellWidth(180);
  211. lstUser.setFixedCellHeight(18);
  212. spUser = new JScrollPane();
  213. spUser.setBackground(Color.decode("#f5f5f5"));
  214. spUser.setFont(new Font("Microsoft YaHei", 0, 12));
  215. spUser.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  216. spUser.getViewport().setView(lstUser);
  217. lblMessage.setBounds(5, 5, 100, 25);
  218. taMessage.setBounds(5, 35, 300, 360);
  219. lblUser.setBounds(310, 5, 100, 25);
  220. spUser.setBounds(310, 35, 220, 360);
  221. lblNotice.setBounds(5, 410, 40, 25);
  222. txtNotice.setBounds(50, 410, 160, 25);
  223. btnSend.setBounds(210, 410, 80, 25);
  224. lblUserCount.setBounds(320, 410, 100, 25);
  225. pnlUser.add(lblMessage);
  226. pnlUser.add(taMessage);
  227. pnlUser.add(lblUser);
  228. pnlUser.add(spUser);
  229. list = new JList<>();
  230. list.setListData(new String[] { "" });
  231. spUser.setViewportView(list);
  232. pnlUser.add(lblNotice);
  233. pnlUser.add(txtNotice);
  234. pnlUser.add(btnSend);
  235. pnlUser.add(lblUserCount);
  236. // 主标签面板
  237. tpServer = new JTabbedPane(JTabbedPane.LEFT);
  238. tpServer.setBackground(Color.decode("#f5f5f5"));
  239. tpServer.setFont(new Font("Microsoft YaHei", 0, 12));
  240. tpServer.add("服务器管理", pnlServer);
  241. tpServer.add("用户信息管理", pnlUser);
  242. this.getContentPane().add(tpServer);
  243. setVisible(true);
  244. }
  245. protected void serverMessage() {
  246. this.serverMessage = txtNotice.getText();
  247. txtNotice.setText("");
  248. }
  249. protected void closeServer() {
  250. this.dispose();
  251. }
  252. protected void saveLog() {
  253. try {
  254. FileOutputStream fileoutput = new FileOutputStream("log.txt", true);
  255. String temp = taMessage.getText();
  256. fileoutput.write(temp.getBytes());
  257. fileoutput.close();
  258. JOptionPane.showMessageDialog(null, "记录保存在log.txt");
  259. } catch (Exception e) {
  260. System.out.println(e);
  261. }
  262. }
  263. private void log(String string) {
  264. String newta = taMessage.getText();
  265. newta += ("\n" + string);
  266. taMessage.setText(newta);
  267. }
  268. public void actionPerformed(ActionEvent evt) {
  269. }
  270. public static void main(String args[]) {
  271. new ServerFrame();
  272. }
  273. }