package com.wenqier.chat.server;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.wenqier.chat.util.XMLUtil;
public class Server extends JFrame {
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JButton jButton;
private JPanel jPanel1;
private JPanel jPanel2;
private JScrollPane jScrollPane;
private JTextArea jTextArea;
private JTextField jTextField;
private Map<String, ServerMessageThread> map = new HashMap<String, ServerMessageThread>();
public Server(String name)
{
super(name);
this.initComponents(); //initialize UI
}
public Map<String, ServerMessageThread> getMap()
{
return map;
}
public JLabel getJLabel2()
{
return jLabel2;
}
public JButton getJButton()
{
return jButton;
}
public JTextArea getJTextArea()
{
return jTextArea;
}
public JTextField getJTextField()
{
return jTextField;
}
public void setJTextField(JTextField textField)
{
jTextField = textField;
}
private void initComponents()
{
jPanel1 = new JPanel();
jPanel2 = new JPanel();
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jTextField = new JTextField(10);
jButton = new JButton();
jScrollPane = new JScrollPane();
jTextArea = new JTextArea();
jPanel1.setBorder(BorderFactory.createTitledBorder("服务器信息"));
jPanel2.setBorder(BorderFactory.createTitledBorder("在线用户列表"));
jTextField.setText("5050");
jLabel1.setText("服务器状态");
jLabel2.setText("停止");
jLabel2.setForeground(new Color(204, 0, 51));
jLabel3.setText("端口号");
jButton.setText("启动服务器");
jButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Server.this.execute(event);
}
});
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
try
{
Collection<ServerMessageThread> cols = Server.this.map.values();
String messageXML = XMLUtil.constructCloseServerWindowXML();
for(ServerMessageThread smt : cols)
{
smt.sendMessage(messageXML);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
System.exit(0);
}
}
});
jPanel1.add(jLabel1);
jPanel1.add(jLabel2);
jPanel1.add(jLabel3);
jPanel1.add(jTextField);
jPanel1.add(jButton);
jTextArea.setEditable(false); //不允许用户手动修改在线用户列表
jTextArea.setRows(20);
jTextArea.setColumns(30);
jTextArea.setForeground(new Color(0, 51, 204));
jScrollPane.setViewportView(jTextArea); //将JTextArea放置到JScrollPane中
jPanel2.add(jScrollPane);
this.getContentPane().add(jPanel1, BorderLayout.NORTH);
this.getContentPane().add(jPanel2, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setAlwaysOnTop(true);
this.setResizable(false);
this.pack();
this.setVisible(true);
}
private void execute(ActionEvent evt)
{
int port = Integer.parseInt(this.getJTextField().getText());
new ServerConnection(this, port).start();
}
public static void main(String[] args)
{
new Server("服务器");
}
}
最近下载更多
13133117021 LV5
2024年12月26日
zeng1206 LV7
2023年12月28日
hentai233 LV1
2023年11月17日
vitcheu LV1
2023年5月27日
Kevin_001 LV6
2022年7月3日
wxh1234567 LV4
2022年6月20日
106sadadwd LV2
2022年6月18日
haneri LV1
2022年6月5日
GaaraNi LV1
2022年4月21日
炸蝎子奥特 LV1
2022年2月12日
最近浏览更多
13133117021 LV5
2024年12月26日
Reach_hehe LV1
2024年12月6日
qwertasdfgkwuejwjwjw LV1
2024年6月27日
ethan_
2024年5月29日
暂无贡献等级
28128觅北 LV2
2024年5月22日
krispeng LV15
2024年4月15日
woldxy LV12
2024年4月1日
taoshen95 LV16
2024年1月18日
zeng1206 LV7
2023年12月28日
陈小灏 LV18
2023年12月27日

