📄 serverframe.java
字号:
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class ServerFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2837827850953733407L;
Server server =null;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JPanel jPanel6 = new JPanel();
JPanel jPanel7 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton8 = new JButton();
JLabel jLabel3 = new JLabel();
List list1 = new List();
List list2 = new List();
BorderLayout borderLayout2 = new BorderLayout();
BorderLayout borderLayout3 = new BorderLayout();
BorderLayout borderLayout4 = new BorderLayout();
GridLayout gridLayout1 = new GridLayout();
GridLayout gridLayout2 = new GridLayout();
JTextArea jsend = new JTextArea(6,40);
JCheckBox jCheckBox1 = new JCheckBox();
public ServerFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception
{
getContentPane().setLayout(borderLayout1);
jsend.setText("注意言行");
jButton7.setText("发送");
jButton7.addActionListener(new ServerFrame_jButton7_actionAdapter(this));
jButton1.setText("启动服务");
jButton1.addActionListener(new ServerFrame_jButton1_actionAdapter(this));
jButton2.setText("关闭服务");
jButton2.addActionListener(new ServerFrame_jButton2_actionAdapter(this));
jLabel3.setText("在线用户列表:");
jPanel2.setLayout(borderLayout2);
jButton4.setText("断开连接");
jButton4.addActionListener(new ServerFrame_jButton4_actionAdapter(this));
jPanel4.setLayout(borderLayout3);
jButton6.setText("保存消息");
jButton6.addActionListener(new ServerFrame_jButton6_actionAdapter(this));
jPanel6.setLayout(gridLayout1);
gridLayout1.setColumns(1);
gridLayout1.setRows(3);
jPanel7.setLayout(gridLayout2);
gridLayout2.setColumns(1);
gridLayout2.setRows(5);
jPanel5.setLayout(borderLayout4);
jButton8.setText("清空消息");
jButton8.addActionListener(new ServerFrame_jButton8_actionAdapter(this));
jCheckBox1.setSelected(true);
jCheckBox1.setText("对所有人");
this.getContentPane().add(jPanel2, java.awt.BorderLayout.WEST);
jPanel2.add(list1, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jPanel3, java.awt.BorderLayout.SOUTH);
jPanel3.add(jsend);
jPanel3.add(jButton7);
this.getContentPane().add(jPanel4, java.awt.BorderLayout.EAST);
jPanel6.add(jButton6, null);
jPanel6.add(jButton8);
jPanel7.add(jButton1);
jPanel7.add(jButton2);
jPanel7.add(jButton4);
jPanel4.add(jPanel6, java.awt.BorderLayout.NORTH);
jPanel4.add(jPanel7,java.awt.BorderLayout.SOUTH);
jButton3.setText("修改昵称");
jButton3.addActionListener(new ServerFrame_jButton3_actionAdapter(this));
jPanel7.add(jButton3);
jButton5.setText("查看历史消息");
jButton5.addActionListener(new ServerFrame_jButton5_actionAdapter(this));
jPanel6.add(jButton5, null);
this.getContentPane().add(jPanel5, java.awt.BorderLayout.CENTER);
jPanel5.add(list2, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel2.add(jLabel3, java.awt.BorderLayout.NORTH);
jPanel2.add(jCheckBox1, java.awt.BorderLayout.SOUTH);
this.setTitle("聊天室服务器端");
this.setSize(850,650);
this.setLocation(100,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
ServerFrame clientframe = new ServerFrame();
}
public void jButton1_actionPerformed(ActionEvent e)
{
try
{
server = new Server(9999, list1,list2);
list2.add("服务器成功启动");
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(this,"启动服务器时出现错误,请检查并重新启动!","提示",JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
public void jButton2_actionPerformed(ActionEvent e)
{
server.destroy();
list2.add("服务器成功关闭");
}
public void jButton8_actionPerformed(ActionEvent e)
{
if(JOptionPane.showConfirmDialog(this,"确定要清空所有信息吗?","清空信息",JOptionPane.YES_NO_OPTION)==0)
{
this.list2.removeAll();
}
}
public void jButton7_actionPerformed(ActionEvent e)
{
if(jsend.getText().length()>0)
{
try
{
if(jCheckBox1.isSelected())
{
this.server.send(jsend.getText());
}
else
{
if(list1.getSelectedItem()==null)
{
JOptionPane.showMessageDialog(this,"请选择要发送信息的用户,或者选中“对所有人”按钮!","提示",JOptionPane.ERROR_MESSAGE);
}
else
{
this.server.send("SPECIAL:"+list1.getSelectedItem()+"$SPECIAL$"+jsend.getText());
this.list2.add("系统管理员 悄悄对 "+list1.getSelectedItem()+" 说:"+jsend.getText());
}
}
}
catch (IOException ex)
{
this.list2.add("发送广播时出错!");
}
}
}
//强制修改用户昵称
public void jButton3_actionPerformed(ActionEvent e)
{
if(list1.getSelectedItem()==null)
{
JOptionPane.showMessageDialog(this,"请先选择要修改昵称的客户!","提示",JOptionPane.ERROR_MESSAGE);
}
else
{
this.server.changeNickname(list1.getSelectedItem(),JOptionPane.showInputDialog("请输入昵称:"));
}
}
public void jButton4_actionPerformed(ActionEvent e)
{
if(list1.getSelectedItem()==null)
{
JOptionPane.showMessageDialog(this,"请先选择要断开连接的客户端!","提示",JOptionPane.ERROR_MESSAGE);
}
else
{
if(JOptionPane.showConfirmDialog(this,"确定要踢掉该用户吗?","提示",JOptionPane.YES_NO_OPTION)==0)
{
server.tickSocket(list1.getSelectedItem());
}
}
}
public void jButton6_actionPerformed(ActionEvent e)
{
JFileChooser fileSave=new JFileChooser();
if(fileSave.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
{
File file = fileSave.getSelectedFile();
try
{
PrintStream ps = new PrintStream(new FileOutputStream(file));
for (int i = 0; i < list2.getItemCount(); i++)
{
ps.println(list2.getItem(i));
}
ps.close();
}
catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(this, "保存失败!", "提示",JOptionPane.ERROR_MESSAGE);
}
JOptionPane.showMessageDialog(this, "保存成功!", "提示",JOptionPane.ERROR_MESSAGE);
}
}
public void jButton5_actionPerformed(ActionEvent e)
{
JFileChooser fileOpen=new JFileChooser();
if(fileOpen.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
{
File file = fileOpen.getSelectedFile();
try
{
BufferedReader br = new BufferedReader(new FileReader(file));
String s = "";
while ((s = br.readLine()) != null)
{
list2.add(s);
}
br.close();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(this, "打开失败!", "提示",JOptionPane.ERROR_MESSAGE);
}
}
}
}
class ServerFrame_jButton5_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton5_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton5_actionPerformed(e);
}
}
class ServerFrame_jButton6_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton6_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton6_actionPerformed(e);
}
}
class ServerFrame_jButton4_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton4_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton4_actionPerformed(e);
}
}
class ServerFrame_jButton3_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton3_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton3_actionPerformed(e);
}
}
class ServerFrame_jButton7_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton7_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton7_actionPerformed(e);
}
}
class ServerFrame_jButton8_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton8_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton8_actionPerformed(e);
}
}
class ServerFrame_jButton2_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton2_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton2_actionPerformed(e);
}
}
class ServerFrame_jButton1_actionAdapter implements ActionListener
{
private ServerFrame adaptee;
ServerFrame_jButton1_actionAdapter(ServerFrame adaptee)
{
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e)
{
adaptee.jButton1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -