package server;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
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 utilpackage.XMLUtil;
class MainServer extends JFrame implements WindowListener{
private JPanel northPanel; //启动区面板
private JPanel onlinePanel; //在线用户区面板
private JPanel diaryPanel; //系统日志区面板
private JPanel centerPanel; //中间部分面板
private JLabel label1,label2,label3;
private JTextField portField; //端口输入区域
private JButton jButton;
private JTextArea userArea; //用户列表
private JTextArea diaryArea; //系统日志
private ServerThread serverThread;
private boolean isStarted = false;
private DateFormat dateFormat;
private HashMap<String, MessageThread> userThreadMap = new HashMap<String, MessageThread>();
public MainServer(){
dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
addWindowListener(this);
initUI();
}
private void initUI(){
setTitle("服务端!"); //设置标题
Container container = getContentPane();
northPanel = new JPanel();
onlinePanel = new JPanel();
diaryPanel = new JPanel();
centerPanel = new JPanel();
/**
* 启动栏界面设置
*/
northPanel.setLayout(new FlowLayout());
northPanel.setSize(400, 80);
label1 = new JLabel("port: ");
portField = new JTextField("5000");
portField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
startAndStopServer();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}); //给 端口输入框添加事件监听器
jButton = new JButton("启动");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
startAndStopServer();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}); //给 “启动” 按钮添加事件监听器
label2 = new JLabel(" 状态 : ");
label3 = new JLabel(" 停止 ");
label3.setForeground(Color.RED);
northPanel.add(label1);
northPanel.add(portField);
northPanel.add(jButton);
northPanel.add(label2);
northPanel.add(label3);
centerPanel.setLayout(new BorderLayout());
onlinePanel.setBorder(BorderFactory.createTitledBorder("在线用户:"));
diaryPanel.setBorder(BorderFactory.createTitledBorder("系统日志:"));
onlinePanel.setLayout(new BorderLayout());
diaryPanel.setLayout(new BorderLayout());
centerPanel.add(onlinePanel,BorderLayout.WEST);
centerPanel.add(diaryPanel,BorderLayout.CENTER);
userArea = new JTextArea();
userArea.setColumns(12);
userArea.setEditable(false);
diaryArea = new JTextArea();
diaryArea.setEditable(false);
onlinePanel.add(new JScrollPane(userArea));
diaryPanel.add(new JScrollPane(diaryArea));
northPanel.setBorder(BorderFactory.createTitledBorder("启动区"));
container.setLayout(new BorderLayout());
container.add(centerPanel,BorderLayout.CENTER);
container.add(northPanel,BorderLayout.NORTH);
/**
* 设置程序大小及位置
*/
int width = 600;
int height = 500;
setVisible(true);
setSize(width,height);
setResizable(false);
int locationX = (getToolkit().getScreenSize().width - width)/2;
int locationY = (getToolkit().getScreenSize().height - height)/2;
setLocation(locationX,locationY);
}
public boolean isUsernameExist(String name){ //判断name是否已经存在
return userThreadMap.containsKey(name);
}
public void addUser(String key,MessageThread value){
userThreadMap.put(key, value);
}
public void deleteUser(String name){
userThreadMap.remove(name);
}
public void sendMessageToAll(String msg) throws IOException, InterruptedException {
for (String key : userThreadMap.keySet()) {
userThreadMap.get(key).sendMessage(msg);
System.out.println("to ALL" + msg);
}
}
public void sendMessageToOther(String msg,MessageThread thread) throws IOException, InterruptedException{
for (String key : userThreadMap.keySet()) {
if (!userThreadMap.get(key).equals(thread)) {
userThreadMap.get(key).sendMessage(msg);
}
}
}
public void newUserOnline(String username){
String msg =dateFormat.format(new Date()) + ": 用户:\"" + username +"\"" + " 上线\n";
diaryArea.append(msg);
}
public void oldUserOffline(String username){
String msg =dateFormat.format(new Date()) + ": 用户:\"" + username +"\"" + " 下线\n";
diaryArea.append(msg);
}
public void updateUserList(){
userArea.setText("");
for(String users : userThreadMap.keySet()){
String msg = users + "\n";
userArea.append(msg);
}
}
public void sendUserListXML() throws IOException, InterruptedException{
String userListXML = XMLUtil.constructUserListXML(userThreadMap);
sendMessageToAll(userListXML);
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
try {
stopServer();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
private void stopServer() throws InterruptedException{
try {
String closeMessage = XMLUtil.constructReplyLoginXML("serverClose");
sendMessageToAll(closeMessage);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void startAndStopServer() throws IOException, InterruptedException{
if(!isStarted){
portField.setEditable(false);
label3.setText("启动");
jButton.setText("停止");
String port = portField.getText();
serverThread = new ServerThread(Integer.parseInt(port),this);
serverThread.start();
diaryArea.append(dateFormat.format(new Date()) + ": 开启服务器\n");
isStarted = true;
}
else{
label3.setText("停止");
stopServer();
serverThread.stopSocket();
portField.setEditable(true);
jButton.setText("启动");
diaryArea.append(dateFormat.format(new Date()) + ": 关闭服务器\n");
isStarted = false;
}
}
public String getPort(){
return portField.getText(
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
chatroom.zip (64个子文件)
程序
jar
jdom.jar 149KB
Server.jar 70KB
Client.jar 80KB
chatroom
bin
client
ClientThread.class 4KB
MessageBox$1.class 804B
MainClient.class 5KB
MessageBox.class 5KB
server
ServerThread.class 1KB
MessageThread.class 4KB
MainServer$2.class 939B
MainServer.class 8KB
MainServer$1.class 939B
utilpackage
XMLUtil.class 6KB
clientManifest 80B
.settings
org.eclipse.jdt.core.prefs 629B
src
client
MessageBox.java 4KB
MainClient.java 4KB
ClientThread.java 4KB
server
MessageThread.java 3KB
ServerThread.java 1KB
MainServer.java 8KB
utilpackage
XMLUtil.java 5KB
.project 384B
.classpath 374B
doc
package-list 21B
server
package-summary.html 6KB
ServerThread.html 13KB
MessageThread.html 13KB
package-frame.html 997B
class-use
ServerThread.html 6KB
MessageThread.html 7KB
package-tree.html 6KB
package-use.html 6KB
deprecated-list.html 5KB
help-doc.html 9KB
allclasses-frame.html 1KB
overview-summary.html 6KB
index.html 1KB
allclasses-noframe.html 1KB
resources
inherit.gif 57B
overview-frame.html 1KB
stylesheet.css 1KB
constant-values.html 5KB
utilpackage
package-summary.html 6KB
package-frame.html 994B
class-use
XMLUtil.html 6KB
Test.html 6KB
package-tree.html 6KB
XMLUtil.html 16KB
Test.html 9KB
package-use.html 5KB
index-files
index-7.html 6KB
index-1.html 7KB
index-8.html 6KB
index-6.html 6KB
index-5.html 7KB
index-3.html 6KB
index-2.html 7KB
index-4.html 6KB
MSG (1).WAV 14KB
overview-tree.html 6KB
voice
newMessage.wav 598B
systemMessage.wav 5KB
manifest 80B
共 64 条
- 1
Android_
- 粉丝: 8
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Python的图像阴影检测与去除源码(高分期末大作业项目).zip
- 基于C++/Qt实现的井字棋游戏
- 基于 Python 编程语言的 Web 框架Django
- Python和Flask实现的基于体检数据的城市公共健康可视分析系统源码+使用说明.zip
- 基于python实现的华为智慧工地-安全帽检测
- buck-boost_2023-12-16_12-12-13.eprj
- 后端开发关于数据库和API开发的介绍
- 机器学习和数据挖掘课程设计-米其林餐厅数据挖掘管理系统源码+使用文档说明.zip
- html html html展示我与ai的对化
- 数据结构课程设计-全国交通出行咨询模拟系统C语言实现源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页