⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 servergui.java

📁 使用TCP和UDP协议
💻 JAVA
字号:
/*
 * Created on 2005-6-4
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package server;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ServerGUI extends JFrame{
    int tcpPort = 1234,
    	udpPort = 1235;
    JPanel panel = null;
    JTextArea displayArea = null;
    JScrollPane displayPane = null;
    JButton startButton = null,
    		stopButton = null;
    JMenuBar serverMenu = null;
    TCPThread tcpThread = null;
    JabberThread jabberThread = null;
    //防止多次调用TCPThread的closeTcpSocket():
    boolean stopped = true;
    
    public ServerGUI()
    {
        initFrame();
        initPanel();
        initMenu();
        this.setJMenuBar(serverMenu);
        this.setContentPane(panel);
    }
    
    void initFrame()
	{
	    this.addWindowListener(new WindowAdapter() {
	        public void windowClosing(WindowEvent frameEvent)
	        {
	            if (!stopped)
                {
                    stopService();
                    stopped = true;
                }
                System.exit(0);
	        }
	    });
		this.setSize(500, 550);
		this.setTitle("\"别问我是谁,请与我面对\"服务器");
	}
    
    void initMenu()
    {
        serverMenu = new JMenuBar();
        
        JMenu fileMenu = new JMenu("文件");
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                if (!stopped)
                {
                    stopService();
                    stopped = true;
                }
                System.exit(0);
            }
        });
        fileMenu.add(exitItem);
        
        JMenu settingMenu = new JMenu("选项");
		JMenuItem tcpItem = new JMenuItem("TCP端口..");
		JMenuItem udpItem = new JMenuItem("UDP端口..");
		tcpItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
		        int temp = ServerGUI.this.tcpPort;
		        try
		        {
		            ServerGUI.this.tcpPort = Integer.parseInt(JOptionPane.showInputDialog("Please input tcp port number"));
		        } catch(NumberFormatException ne)
		        {
		            ServerGUI.this.appendDisplayArea("请输入数字");
		            ServerGUI.this.tcpPort = temp;
		        }
		        if (ServerGUI.this.tcpPort > 65536 || ServerGUI.this.tcpPort < 1024)
		        {
		            ServerGUI.this.appendDisplayArea("请选择1024到65535范围内的数字");
		            ServerGUI.this.tcpPort = temp;
		        }
		    }
		});
		udpItem.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e)
		    {
		        int temp = ServerGUI.this.udpPort;
		        try
		        {
		            ServerGUI.this.udpPort = Integer.parseInt(JOptionPane.showInputDialog("Please input udp port number"));
		        } catch(NumberFormatException ne)
		        {
		            ServerGUI.this.appendDisplayArea("Invalid input");
		            ServerGUI.this.udpPort = temp;
		        }
		        if (ServerGUI.this.udpPort > 65536 || ServerGUI.this.udpPort < 1024)
		        {
		            ServerGUI.this.appendDisplayArea("请选择1024到65535范围内的数字");
		            ServerGUI.this.udpPort = temp;
		        }
		    }
		});
		settingMenu.add(tcpItem);
		settingMenu.add(udpItem);
		
		serverMenu.add(fileMenu);
		serverMenu.add(settingMenu);
    }
    
    public void stopService()
    {
        tcpThread.messageToAll("SERVERQUIT");
        if (tcpThread != null)
        {
            tcpThread.closeTcpSocket();
            tcpThread.stopTCPThread();
        }
        if (jabberThread != null)
        {
            jabberThread.closeUdpSocket();
            jabberThread.stopUDPThread();
        }
    }
    
    void initPanel()
    {
        panel = new JPanel(new BorderLayout());
        if (startButton == null)
            startButton = new JButton("启动!");
        startButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                if (stopped)
                {
	                tcpThread = new TCPThread(ServerGUI.this, ServerGUI.this.tcpPort);
	                if (tcpThread.tcpSuccess == false)
	                {
	                    appendDisplayArea("构造TCPThread类时遇到错误,请重新启动");
	                    return ;
	                }
	                tcpThread.start();
	                
	                jabberThread = new JabberThread(ServerGUI.this, ServerGUI.this.tcpThread, ServerGUI.this.udpPort);
	                if (jabberThread.udpSuccess == false)
	                {
	                    appendDisplayArea("构造UDPThread类时遇到错误,请重新启动");
	                    return ;
	                }
	                jabberThread.start();
	                stopped = false;
	                appendDisplayArea("服务器已启动");
                }
            }
        });
        if (stopButton == null)
            stopButton = new JButton("停止!");
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                if (!stopped)
                {
                    stopService();
                    stopped = true;
                } else
                {
                    appendDisplayArea("已经停止");
                }
            }
        });
        Box buttons = Box.createHorizontalBox();
        buttons.add(Box.createHorizontalGlue());
        buttons.add(startButton);
        buttons.add(Box.createHorizontalGlue());
        buttons.add(stopButton);
        buttons.add(Box.createHorizontalGlue());
        
        if (displayPane == null)
        {
            displayArea = new JTextArea();
            displayArea.setLineWrap(true);
            displayPane = new JScrollPane(displayArea, 
        			JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        			JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        }
        
        panel.add(BorderLayout.NORTH, buttons);
        panel.add(BorderLayout.CENTER, displayPane);
    }
    
    public void setDisplayArea(String text)
    {
        displayArea.setText(text);
    }
    public void appendDisplayArea(String text)
    {
        displayArea.append(text + "\n");
    }
    public static void main(String[] args)
    {
        ServerGUI serverGUI = new ServerGUI();
        serverGUI.show();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -