📄 eserverframe.java
字号:
S.mi1.addActionListener(this);
S.mi2.addActionListener(this);
S.mi3.addActionListener(this);
S.mi4.addActionListener(this);
S.mi5.addActionListener(this);
S.mi6.addActionListener(this);
S.B.Leave.addMouseListener(this);
S.B.Return.addMouseListener(this);
S.B.Reset.addMouseListener(this);
S.B.Exit.addMouseListener(this);
S.B.Stop.addMouseListener(this);
S.B.Recieve.addMouseListener(this);
S.M.L1.All.setText("");
S.M.L1.setBackground(new Color(122,181,222));
S.M.L1.Title.setIcon(new ImageIcon("BX5.jpg"));
S.M.L1.U.I1.setIcon(new ImageIcon("BY9.jpg"));
S.M.L1.U.UserList.setBackground(new Color(206,242,255));
S.M.L1.U.setBackground(new Color(206,242,255));
S.M.L2.UserLabel.setText("");
S.M.L2.UserLabel.setText(" 名单人数:0");
S.M.L2.UserLabel.setForeground(new Color(0,0,0));
S.M.L2.All.setText("");
S.M.L2.setBackground(new Color(122,181,222));
S.M.L2.Title.setIcon(new ImageIcon("BX6.jpg"));
S.M.L2.U.I1.setIcon(new ImageIcon("BY9.jpg"));
S.M.L2.U.UserList.setBackground(new Color(206,242,255));
S.M.L2.U.setBackground(new Color(206,242,255));
setSize(600,465);
setResizable(false);
show();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit TK = Toolkit.getDefaultToolkit();
Dimension Size = TK.getScreenSize();
this.setBounds((Size.width-this.getSize().width)/2,
(Size.height-this.getSize().height)/2,
this.getSize().width,this.getSize().height);
addWindowListener(new WindowAdapter()//关闭窗口时调用
{
public void windowClosing(WindowEvent e)
{
System.out.println("Close Server!!");
try
{
for(Iterator i=FChatServer.getHashMap().values().iterator(); i.hasNext();)
((ObjectOutputStream)i.next()).writeObject(new FChatInfo ("","CloseServer",null,null,null,null,0,null));
}
catch(IOException ioe)
{}
System.exit(0);
}
});
}
public void run()//计时线程
{
while (true)
{
try
{
time.sleep(1000);
S.B.Clock.setText(displayTime());
}
catch (Exception e)
{}
}
}
public String displayTime()//显示时间
{
String s,ss,sss;
GregorianCalendar g=new GregorianCalendar();
if(g.get(GregorianCalendar.HOUR)<10)//能在小时为个位数时前面加个"0"
{
s="0"+g.get(GregorianCalendar.HOUR);
}
else
{
s=""+g.get(GregorianCalendar.HOUR);
}
if(g.get(GregorianCalendar.MINUTE)<10)//能在分钟为个位数时前面加个"0"
{
ss="0"+g.get(GregorianCalendar.MINUTE);
}
else
{
ss=""+g.get(GregorianCalendar.MINUTE);
}
if(g.get(GregorianCalendar.SECOND)<10)//能在秒为个位数时前面加个"0"
{
sss="0"+g.get(GregorianCalendar.SECOND);
}
else
{
sss=""+g.get(GregorianCalendar.SECOND);
}
return s+":"+ss+":"+sss;
}
public void StartMessage(String M)//服务器启动时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(255,255,128));
S.M.T.Message.add(W);
}
public void CloseMessage(String M)//服务器关闭时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(255,60,60));
S.M.T.Message.add(W);
}
public void LoginMessage(String M)//用户登录时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(128,255,255));
S.M.T.Message.add(W);
}
public void LogoutMessage(String M)//用户登出时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(255,150,200));
S.M.T.Message.add(W);
}
public void LeaveMessage(String M)//用户暂离开时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(242,109,125));
S.M.T.Message.add(W);
}
public void ReturnMessage(String M)//用户返回时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(60,184,120));
S.M.T.Message.add(W);
}
public void StopMessage(String M)//屏蔽用户时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(180,0,180));
S.M.T.Message.add(W);
}
public void RecieveMessage(String M)//恢复屏蔽时显示消息
{
JLabel W=new JLabel(M);
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(0,225,57));
S.M.T.Message.add(W);
}
public void mousePressed(MouseEvent e)
{
Object Obj=e.getSource();
if(Obj==S.B.Leave)
S.B.Leave.setIcon(new ImageIcon("ELeave3.jpg"));
if(Obj==S.B.Return)
S.B.Return.setIcon(new ImageIcon("EReturn3.jpg"));
if(Obj==S.B.Reset)
{
S.B.Reset.setIcon(new ImageIcon("EReset3.jpg"));
S.M.T.Message.setBackground(new Color(240,240,240));
}
if(Obj==S.B.Exit)
S.B.Exit.setIcon(new ImageIcon("EExit3.jpg"));
if(Obj==S.B.Stop)
S.B.Stop.setIcon(new ImageIcon("EStop3.jpg"));
if(Obj==S.B.Recieve)
S.B.Recieve.setIcon(new ImageIcon("BRecieve3.jpg"));
}
public void mouseReleased(MouseEvent e)
{
Object Obj=e.getSource();
if(Obj==S.B.Leave)
S.B.Leave.setIcon(new ImageIcon("ELeave1.jpg"));
if(Obj==S.B.Return)
S.B.Return.setIcon(new ImageIcon("EReturn1.jpg"));
if(Obj==S.B.Reset)
{
S.B.Reset.setIcon(new ImageIcon("EReset1.jpg"));
S.M.T.Message.setBackground(new Color(0,0,0));
}
if(Obj==S.B.Exit)
S.B.Exit.setIcon(new ImageIcon("EExit1.jpg"));
if(Obj==S.B.Stop)
S.B.Stop.setIcon(new ImageIcon("EStop1.jpg"));
if(Obj==S.B.Recieve)
S.B.Recieve.setIcon(new ImageIcon("BRecieve1.jpg"));
}
public void mouseEntered(MouseEvent e)
{
Object Obj=e.getSource();
if(Obj==S.B.Leave)
S.B.Leave.setIcon(new ImageIcon("ELeave2.jpg"));
if(Obj==S.B.Return)
S.B.Return.setIcon(new ImageIcon("EReturn2.jpg"));
if(Obj==S.B.Reset)
S.B.Reset.setIcon(new ImageIcon("EReset2.jpg"));
if(Obj==S.B.Exit)
S.B.Exit.setIcon(new ImageIcon("EExit2.jpg"));
if(Obj==S.B.Stop)
S.B.Stop.setIcon(new ImageIcon("EStop2.jpg"));
if(Obj==S.B.Recieve)
S.B.Recieve.setIcon(new ImageIcon("BRecieve2.jpg"));
}
public void mouseExited(MouseEvent e)
{
Object Obj=e.getSource();
if(Obj==S.B.Leave)
S.B.Leave.setIcon(new ImageIcon("ELeave1.jpg"));
if(Obj==S.B.Return)
S.B.Return.setIcon(new ImageIcon("EReturn1.jpg"));
if(Obj==S.B.Reset)
S.B.Reset.setIcon(new ImageIcon("EReset1.jpg"));
if(Obj==S.B.Exit)
S.B.Exit.setIcon(new ImageIcon("EExit1.jpg"));
if(Obj==S.B.Stop)
S.B.Stop.setIcon(new ImageIcon("EStop1.jpg"));
if(Obj==S.B.Recieve)
S.B.Recieve.setIcon(new ImageIcon("BRecieve1.jpg"));
}
public void mouseClicked(MouseEvent e)//按钮的处理事件
{
Object Obj=e.getSource();
if(Obj==S.B.Leave)//菜单中的断开服务器按钮
{
System.out.println("Stop Server!!");
try
{
for(Iterator i=FChatServer.getHashMap().values().iterator(); i.hasNext();)
((ObjectOutputStream)i.next()).writeObject(new FChatInfo ("","StopServer",null,null,null,null,0,""));
FChatServer.getHashMap().clear();
FChatServer.v.clear();
S.M.L1.UserLabel.setText("在线用户数"+0);//设置在线人数
S.M.L1.U.UserList.removeAll();
S.M.L1.U.UserList.setListData(FChatServer.v);
fcs.StopServer();//关闭服务器套接字
SS="关闭";//设置服务器状态是"off"
S.ZZ.setIcon(new ImageIcon("BS2.jpg"));
CloseMessage("***您已停止了所有客户端的连接***");
}
catch(IOException ioe)
{}
}
if(Obj==S.B.Return)//菜单中的恢复连接按钮
{
if(SS.equals("关闭"))
{
System.out.println("Server restart!!");
StartMessage("***您已恢复了所有客户端的连接***");
fcs.StartServer();//重新连接
SS="开启";//设置服务器状态是"on"
S.ZZ.setIcon(new ImageIcon("BS1.jpg"));
}
else
{
return;
}
}
if(Obj==S.B.Exit)//菜单中的退出按钮
{
DExit3Dialog D=new DExit3Dialog(this,"Exit",true);
}
if(Obj==S.B.Reset)//菜单中的清屏按钮
{
S.M.T.Message.removeAll();
}
if(Obj==S.B.Stop)//菜单中的屏蔽按钮
{
DStop1Dialog D=new DStop1Dialog(this,"屏蔽",true);
}
if(Obj==S.B.Recieve)//菜单中的恢复屏蔽按钮
{
DStop2Dialog D=new DStop2Dialog(this,"",true);
}
}
public void actionPerformed(ActionEvent e)
{
Object Obj=e.getSource();
if(Obj==S.mi4)//菜单中的"关于聊天室"按钮
{
DAboutDialog D=new DAboutDialog(this,"关于聊天室",true);
}
if(Obj==S.mi2)
{
if(SS.equals("关闭"))
{
System.out.println("Server restart!!");
StartMessage("***您已恢复了所有客户端的连接***");
fcs.StartServer();//重新连接
SS="开启";
S.ZZ.setIcon(new ImageIcon("BS1.jpg"));
}
}
if(Obj==S.mi3)//菜单中的退出按钮
{
DExit3Dialog D=new DExit3Dialog(this,"Exit",true);
}
if(Obj==S.mi1)//菜单中的断开服务器按钮
{
System.out.println("Stop Server!!");
try
{
for(Iterator i=FChatServer.getHashMap().values().iterator(); i.hasNext();)
((ObjectOutputStream)i.next()).writeObject(new FChatInfo ("","StopServer",null,null,null,null,0,""));
FChatServer.getHashMap().clear();
FChatServer.v.clear();
S.M.L1.UserLabel.setText("在线用户数"+0);//设置在线人数
S.M.L1.U.UserList.removeAll();
S.M.L1.U.UserList.setListData(FChatServer.v);
fcs.StopServer();//关闭服务器套接字
SS="关闭";
S.ZZ.setIcon(new ImageIcon("BS2.jpg"));
CloseMessage("***您已停止了所有客户端的连接***");
}
catch(IOException ioe)
{}
}
if(Obj==S.mi5)//菜单中的屏蔽按钮
{
DStop1Dialog D=new DStop1Dialog(this,"屏蔽",true);
}
if(Obj==S.mi6)//菜单中的恢复屏蔽按钮
{
DStop2Dialog D=new DStop2Dialog(this,"",true);
}
}
class DExit3Panel extends JPanel//用户退出时的确认对话框
{
JLabel Text,Image,A1;
DButtonPanel B;
GridBagLayout GBL;
GridBagConstraints GBC;
DExit3Panel()
{
Text=new JLabel(new ImageIcon("DT1.jpg"));
Image=new JLabel(new ImageIcon("DI1.gif"));
B=new DButtonPanel();
A1=new JLabel(" ");
A1.setFont(new Font("Trebuchet MS",Font.BOLD,11));
GBC= new GridBagConstraints();
setBackground(new Color(125,193,234));
setLayout(GBL= new GridBagLayout());
GBC.gridx=1;
GBC.gridy=2;
GBL.setConstraints(Image,GBC);
GBC.gridx=1;
GBC.gridy=3;
GBL.setConstraints(Text,GBC);
GBC.gridx=1;
GBC.gridy=4;
GBL.setConstraints(A1,GBC);
GBC.gridx=1;
GBC.gridy=5;
GBL.setConstraints(B,GBC);
add(Image);
add(Text);
add(A1);
add(B);
}
}
class DExit3Dialog extends JDialog implements MouseListener
{
DExit3Panel D;
DExit3Dialog(JFrame C,String S,boolean B)
{
super(C,S,B);
D=new DExit3Panel();
getContentPane().add(D);
D.B.Accept.addMouseListener(this);
D.B.Return.addMouseListener(this);
setLocation(350,250);
setSize(250,230);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -