1 packagecom.silianbo.server;2
3 importcom.silianbo.CaptureScreen;4 importjava.awt.BorderLayout;5 importjava.awt.Container;6 importjava.awt.Dimension;7 importjava.awt.GridBagConstraints;8 importjava.awt.GridBagLayout;9 importjava.awt.Insets;10 importjava.awt.Toolkit;11 importjava.awt.event.ActionEvent;12 importjava.awt.event.ActionListener;13 importjava.awt.event.WindowAdapter;14 importjava.awt.event.WindowEvent;15 importjava.net.ServerSocket;16 importjavax.swing.JButton;17 importjavax.swing.JComboBox;18 importjavax.swing.JFrame;19 importjavax.swing.JLabel;20 importjavax.swing.JMenu;21 importjavax.swing.JMenuBar;22 importjavax.swing.JMenuItem;23 importjavax.swing.JOptionPane;24 importjavax.swing.JPanel;25 importjavax.swing.JScrollPane;26 importjavax.swing.JTextArea;27 importjavax.swing.JTextField;28 importjavax.swing.JToolBar;29 importjavax.swing.UIManager;30 importjavax.swing.UnsupportedLookAndFeelException;31
32 public final class ChatServer extends JFrame implementsActionListener {33
34 private static final long serialVersionUID = 1L;35
36 public static int port = 8888;37
38 ServerSocket serverSocket;39
40 JComboBox combobox;41
42 JTextArea messageShow;43
44 JScrollPane messageScrollPane;45
46 JTextField showStatus;47
48 JLabel sendToLabel, messageLabel;49
50 JTextField sysMessage;51
52 JButton sysMessageButton;53
54 UserLinkList userLinkList;55
56 JMenuBar jMenuBar = newJMenuBar();57
58 JMenu serviceMenu = new JMenu("服务");59
60 JMenuItem portItem = new JMenuItem("端口设置");61
62 JMenuItem startItem = new JMenuItem("启动服务");63
64 JMenuItem stopItem = new JMenuItem("停止服务");65
66 JMenuItem exitItem = new JMenuItem("退出");67
68 JMenu helpMenu = new JMenu("帮助");69
70 JMenuItem helpItem = new JMenuItem("帮助");71
72 JToolBar toolBar = newJToolBar();73
74 JButton portSet;75
76 JButton startServer;77
78 JButton stopServer;79
80 JButton exitButton;81
82 JButton uploadButton;83
84 Dimension faceSize = new Dimension(550, 550);85
86 ServerListen listenThread;87
88 JPanel downPanel;89
90 GridBagLayout girdBag;91
92 GridBagConstraints girdBagCon;93
94 publicChatServer() {95 init();96 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);97 this.pack();98 this.setSize(faceSize);99 Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();100 this.setLocation((int) (screenSize.width - faceSize.getWidth()) / 2, (int) (screenSize.height - faceSize.getHeight()) / 2);101 this.setResizable(false);102 this.setTitle("聊天室服务器端");103 setVisible(true);104 }105
106 public voidinit() {107 Container contentPane =getContentPane();108 contentPane.setLayout(newBorderLayout());109 serviceMenu.add(startItem);110 serviceMenu.add(stopItem);111 serviceMenu.add(exitItem);112 jMenuBar.add(serviceMenu);113 helpMenu.add(helpItem);114 JMenu add =jMenuBar.add(helpMenu);115 setJMenuBar(jMenuBar);116 portSet = new JButton("端口设置");117 startServer = new JButton("启动服务");118 stopServer = new JButton("停止服务");119 exitButton = new JButton("退出");120 toolBar.add(portSet);121 toolBar.addSeparator();122 toolBar.add(startServer);123 toolBar.add(stopServer);124 toolBar.addSeparator();125 toolBar.add(exitButton);126 contentPane.add(toolBar, BorderLayout.NORTH);127 stopServer.setEnabled(false);128 stopItem.setEnabled(false);129 portItem.addActionListener(this);130 startItem.addActionListener(this);131 stopItem.addActionListener(this);132 exitItem.addActionListener(this);133 helpItem.addActionListener(this);134 portSet.addActionListener(this);135 startServer.addActionListener(this);136 stopServer.addActionListener(this);137 exitButton.addActionListener(this);138 combobox = newJComboBox();139 combobox.insertItemAt("所有人", 0);140 combobox.setSelectedIndex(0);141 messageShow = newJTextArea();142 messageShow.setEditable(false);143 messageScrollPane = newJScrollPane(messageShow, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);144 messageScrollPane.setPreferredSize(new Dimension(400, 400));145 messageScrollPane.revalidate();146 showStatus = new JTextField(25);147 showStatus.setEditable(false);148 sysMessage = new JTextField(25);149 sysMessage.setEnabled(false);150 sysMessageButton = newJButton();151 sysMessageButton.setText("发送");152 sysMessage.addActionListener(this);153 sysMessageButton.addActionListener(this);154 sendToLabel = new JLabel("发送至:");155 messageLabel = new JLabel("发送消息:");156 downPanel = newJPanel();157 girdBag = newGridBagLayout();158 downPanel.setLayout(girdBag);159 girdBagCon = newGridBagConstraints();160 girdBagCon.gridx = 0;161 girdBagCon.gridy = 0;162 girdBagCon.gridwidth = 3;163 girdBagCon.gridheight = 2;164 girdBagCon.ipadx = 5;165 girdBagCon.ipady = 5;166 JLabel none = new JLabel(" ");167 girdBag.setConstraints(none, girdBagCon);168 downPanel.add(none);169 girdBagCon = newGridBagConstraints();170 girdBagCon.gridx = 0;171 girdBagCon.gridy = 2;172 girdBagCon.insets = new Insets(1, 0, 0, 0);173 girdBagCon.ipadx = 5;174 girdBagCon.ipady = 5;175 girdBag.setConstraints(sendToLabel, girdBagCon);176 downPanel.add(sendToLabel);177 girdBagCon = newGridBagConstraints();178 girdBagCon.gridx = 1;179 girdBagCon.gridy = 2;180 girdBagCon.anchor =GridBagConstraints.LINE_START;181 girdBag.setConstraints(combobox, girdBagCon);182 downPanel.add(combobox);183 girdBagCon = newGridBagConstraints();184 girdBagCon.gridx = 0;185 girdBagCon.gridy = 3;186 girdBag.setConstraints(messageLabel, girdBagCon);187 downPanel.add(messageLabel);188 girdBagCon = newGridBagConstraints();189 girdBagCon.gridx = 1;190 girdBagCon.gridy = 3;191 girdBag.setConstraints(sysMessage, girdBagCon);192 downPanel.add(sysMessage);193 girdBagCon = newGridBagConstraints();194 girdBagCon.gridx = 2;195 girdBagCon.gridy = 3;196 girdBag.setConstraints(sysMessageButton, girdBagCon);197 downPanel.add(sysMessageButton);198 girdBagCon = newGridBagConstraints();199 girdBagCon.gridx = 0;200 girdBagCon.gridy = 4;201 girdBagCon.gridwidth = 3;202 girdBag.setConstraints(showStatus, girdBagCon);203 downPanel.add(showStatus);204 contentPane.add(messageScrollPane, BorderLayout.CENTER);205 contentPane.add(downPanel, BorderLayout.SOUTH);206 this.addWindowListener(newWindowAdapter() {207
208 @Override209 public voidwindowClosing(WindowEvent e) {210 stopService();211 System.exit(0);212 }213 });214 }215
216 @Override217 public voidactionPerformed(ActionEvent e) {218 Object obj =e.getSource();219 if (obj == startServer || obj ==startItem) {220 startService();221 } else if (obj == stopServer || obj ==stopItem) {222 int j = JOptionPane.showConfirmDialog(this, "真的停止服务吗?", "停止服务", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);223 if (j ==JOptionPane.YES_OPTION) {224 stopService();225 }226 } else if (obj == portSet || obj ==portItem) {227 PortConf portConf = new PortConf(this);228 portConf.setVisible(true);229 } else if (obj == exitButton || obj ==exitItem) {230 int j = JOptionPane.showConfirmDialog(this, "真的要退出吗?", "退出", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE);231 if (j ==JOptionPane.YES_OPTION) {232 stopService();233 System.exit(0);234 }235 } else if (obj ==helpItem) {236 CaptureScreen.RandomName.Help helpDialog = new CaptureScreen.RandomName.Help(this);237 helpDialog.setVisible(true);238 } else if (obj == sysMessage || obj ==sysMessageButton) {239 sendSystemMessage();240 }241 }242
243 public voidstartService() {244 try{245 serverSocket = new ServerSocket(port, 10);246 messageShow.append("服务端已经启动,在" + port + "端口侦听...\n");247 startServer.setEnabled(false);248 startItem.setEnabled(false);249 portSet.setEnabled(false);250 portItem.setEnabled(false);251 stopServer.setEnabled(true);252 stopItem.setEnabled(true);253 sysMessage.setEnabled(true);254 } catch(Exception e) {255 }256 userLinkList = newUserLinkList();257 listenThread = newServerListen(serverSocket, combobox, messageShow, showStatus, userLinkList);258 listenThread.start();259 }260
261 public voidstopService() {262 try{263 sendStopToAll();264 listenThread.isStop = true;265 serverSocket.close();266 int count =userLinkList.getCount();267 int i = 0;268 while (i
289 public voidsendStopToAll() {290 int count =userLinkList.getCount();291 int i = 0;292 while (i
307 public voidsendMsgToAll(String msg) {308 int count =userLinkList.getCount();309 int i = 0;310 while (i
328 public voidsendSystemMessage() {329 String toSomebody =combobox.getSelectedItem().toString();330 String message = sysMessage.getText() + "\n";331 messageShow.append(message);332 if (toSomebody.equalsIgnoreCase("所有人")) {333 sendMsgToAll(message);334 } else{335 Node node;336 node =userLinkList.findUser(toSomebody);337 try{338 node.output.writeObject("系统信息");339 node.output.flush();340 node.output.writeObject(message);341 node.output.flush();342 } catch(Exception e) {343 }344 sysMessage.setText("");345 }346 }347
348 public static voidmain(String[] args) {349 try{350 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());351 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException |UnsupportedLookAndFeelException e) {352 }353 ChatServer chatServer = newChatServer();354 }355 }