package chatroom;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
public class ServerControl extends JFrame {
JPanel contentPane;
JButton btnStart = new JButton();
JLabel lblState = new JLabel();
TitledBorder titledBorder1 = new TitledBorder("");
JScrollPane srpList = new JScrollPane();
DefaultListModel dlmUsers = new DefaultListModel();
JList lstUsers = new JList(dlmUsers);
JScrollPane srpChatMessage = new JScrollPane();
DefaultListModel dlmChat = new DefaultListModel();
JList lstChat = new JList(dlmChat);
ServerSocket ss;
//端口号8080
public static int PORT = 9000;
public boolean flag = true;
JTextField txtMsg = new JTextField();
JComboBox cmbType = new JComboBox();
JButton btnSend = new JButton();
JButton btnKick = new JButton();
JButton btnSave = new JButton();
JButton btnClear = new JButton();
public ServerControl() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
lstChat.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
lstChat.setAutoscrolls(true);
this.srpChatMessage.setAutoscrolls(true);
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(395, 500));
setResizable(false);
setTitle("服务器端");
btnStart.setBounds(new Rectangle(19, 16, 103, 27));
btnStart.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnStart.setText("启动服务器");
btnStart.addActionListener(new ServerControl_btnStart_actionAdapter(this));
lblState.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
lblState.setForeground(Color.red);
Border border = BorderFactory.createEtchedBorder(Color.white,
new Color(170, 170, 170));
lblState.setBorder(new TitledBorder(border, "服务器状态",
TitledBorder.ABOVE_TOP,
TitledBorder.CENTER,
new Font("Dialog", Font.PLAIN, 12)));
lblState.setHorizontalAlignment(SwingConstants.CENTER);
lblState.setText("关闭");
lblState.setBounds(new Rectangle(142, 8, 230, 34));
srpList.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
srpList.setBorder(new TitledBorder(border, "用户列表",
TitledBorder.ABOVE_TOP,
TitledBorder.CENTER,
new Font("Dialog", Font.PLAIN, 12)));
srpList.setBounds(new Rectangle(23, 62, 96, 287));
srpChatMessage.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
srpChatMessage.setBorder(new TitledBorder(border, "聊天记录",
TitledBorder.ABOVE_TOP,
TitledBorder.CENTER,
new Font("Dialog", Font.PLAIN,
12)));
srpChatMessage.setBounds(new Rectangle(140, 62, 235, 284));
txtMsg.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
txtMsg.setText("");
txtMsg.setBounds(new Rectangle(27, 390, 218, 27));
cmbType.addItem("群聊");
cmbType.addItem("私聊");
cmbType.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
cmbType.setBounds(new Rectangle(264, 390, 106, 27));
cmbType.setBorder(new LineBorder(Color.GRAY));
btnSend.setBounds(new Rectangle(263, 426, 110, 21));
btnSend.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnSend.setText("发送消息");
btnSend.addActionListener(new ServerControl_btnSend_actionAdapter(this));
btnKick.setBounds(new Rectangle(28, 357, 78, 21));
btnKick.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnKick.setText("踢人");
btnKick.addActionListener(new ServerControl_btnKick_actionAdapter(this));
btnSave.setBounds(new Rectangle(118, 357, 119, 21));
btnSave.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnSave.setText("保存聊天记录");
btnSave.addActionListener(new ServerControl_btnSave_actionAdapter(this));
btnClear.setBounds(new Rectangle(251, 357, 119, 21));
btnClear.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
btnClear.setText("清除聊天信息");
btnClear.addActionListener(new ServerControl_btnClear_actionAdapter(this));
lstUsers.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
contentPane.add(btnStart);
contentPane.add(srpList);
contentPane.add(srpChatMessage);
contentPane.add(lblState);
contentPane.add(btnKick);
contentPane.add(btnSave);
contentPane.add(btnClear);
contentPane.add(cmbType);
contentPane.add(txtMsg);
contentPane.add(btnSend);
srpChatMessage.getViewport().add(lstChat);
srpList.getViewport().add(lstUsers);
lstUsers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}
//启动按钮
public void btnStart_actionPerformed(ActionEvent e) {
String text = btnStart.getText();
if (text.equals("启动服务器")) {
startServer();
} else {
stopServer();
}
}
//启动服务器
public void startServer() {
try {
flag=true;
ss = new ServerSocket(PORT);
lblState.setText("服务器已经启动,在侦听" + PORT + "端口");
btnStart.setText("关闭服务器");
new ListenerThread().start();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "服务器启动失败!");
}
}
//关闭服务器
public void stopServer() {
flag = false;
lblState.setText("服务器已经关闭");
btnStart.setText("启动服务器");
try {
DefaultListModel dlm=(DefaultListModel)lstUsers.getModel();
//采用枚举集合
Enumeration emu=dlm.elements();
while(emu.hasMoreElements())
{
((ClientBean)emu.nextElement()).getS().close();
}
dlm.clear();
ss.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "关闭服务器产生错误!");
}
}
//踢人功能
public void btnKick_actionPerformed(ActionEvent e) {
Object obj = lstUsers.getSelectedValue();
if (obj != null) {
ClientBean cb = (ClientBean) obj;
((DefaultListModel) lstUsers.getModel()).removeElement(cb);
try {
cb.getS().close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
String msg = "管理员将【" + cb.getClientName() + "】赶出了聊天室";
sendAll(msg);
sendList();
}
} else {
JOptionPane.showMessageDialog(null, "请从用户列表中选择您要踢出的人!");
}
}
//保存聊天日志
public void btnSave_actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
jfc.setAcceptAllFileFilterUsed(false);
jfc.setFileFilter(new MyFileFilter());
int choice = jfc.showSaveDialog(this);
if (choice == jfc.APPROVE_OPTION) {
File destnatio