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

📄 qqserver.java

📁 Java+Eclipse+Mysql的局域网聊天程序源代码
💻 JAVA
字号:
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JViewport;
import javax.swing.JToolBar;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JRadioButton;
import java.awt.Insets;
import javax.swing.ImageIcon;
import java.awt.event.*;
import java.util.Date;

import javax.swing.DefaultListModel;

public class QQServer extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JMenuBar jJMenuBar = null;

	private JMenu jMenu = null;

	private JMenuItem jMenuItem = null;

	private JViewport jViewport = null;

	private JToolBar jJToolBarBar = null;

	private JRadioButton jr_Start = null;

	private JRadioButton jr_Pause = null;

	private JRadioButton jr_Stop = null;

	private JPanel jPanel = null;

	private JScrollPane jScrollPane = null;

	private JList jl_User = null;

	private JPanel jPanel1 = null;

	private JLabel jLabel = null;

	private JLabel jl_Logsum = null;

	private JLabel jLabel1 = null;

	private JLabel jl_ServerAddr = null;

	private JLabel jLabel2 = null;

	private JLabel jl_Port = null;

	private JLabel jLabel3 = null;

	private JLabel jl_state = null;
	
	public DefaultListModel dlm_User=null;
	
	public ReceivMsg rm=null; 
	public SendMsg sm=null;
	
	public ListCellInfo lci=null;     //已登录用户队列
	public ListCellInfo lciend=null;
	public int lcinum=0;
	
	private ReadConfig rc=null;
	

	private int Server_port=0;

	private JMenu jMenu1 = null;

	private JMenuItem jm_Serverset = null;

	private JMenuItem jm_Accessset = null;
	
	private ServerSet ss=null;
	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
			jJMenuBar.setPreferredSize(new Dimension(45, 25));
			jJMenuBar.add(getJMenu());
			jJMenuBar.add(getJMenu1());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			jMenu.setText("文件");
			jMenu.add(getJMenuItem());
		}
		return jMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem() {
		if (jMenuItem == null) {
			jMenuItem = new JMenuItem();
			jMenuItem.setText("退出");
		}
		return jMenuItem;
	}

	/**
	 * This method initializes jViewport	
	 * 	
	 * @return javax.swing.JViewport	
	 */
	private JViewport getJViewport() {
		if (jViewport == null) {
			jViewport = new JViewport();
		}
		return jViewport;
	}

	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getJJToolBarBar() {
		if (jJToolBarBar == null) {
			jJToolBarBar = new JToolBar();
			jJToolBarBar.setPreferredSize(new Dimension(56, 25));
			jJToolBarBar.add(getJr_Start());
			jJToolBarBar.add(getJr_Pause());
			jJToolBarBar.add(getJr_Stop());
		}
		return jJToolBarBar;
	}

	/**
	 * This method initializes jr_Start	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJr_Start() {
		if (jr_Start == null) {
			jr_Start = new JRadioButton();
			jr_Start.setText("开始");
			jr_Start.setIcon(new ImageIcon(getClass().getResource("/start.png")));
			jr_Start.addActionListener(this);
		}
		return jr_Start;
	}

	/**
	 * This method initializes jr_Pause	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJr_Pause() {
		if (jr_Pause == null) {
			jr_Pause = new JRadioButton();
			jr_Pause.setText("暂停");
			jr_Pause.setIcon(new ImageIcon(getClass().getResource("/pause.png")));
			jr_Pause.setEnabled(false);
			jr_Pause.addActionListener(this);
		}
		return jr_Pause;
	}

	/**
	 * This method initializes jr_Stop	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJr_Stop() {
		if (jr_Stop == null) {
			jr_Stop = new JRadioButton();
			jr_Stop.setText("停止");
			jr_Stop.setIcon(new ImageIcon(getClass().getResource("/stop.png")));
			jr_Stop.setEnabled(false);
			jr_Stop.addActionListener(this);
		}
		return jr_Stop;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints.weightx = 1.0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJScrollPane(), gridBagConstraints);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJl_User());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jl_User	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJl_User() {
		if (jl_User == null) {
			jl_User = new JList();	
			jl_User.setModel(dlm_User);
			jl_User.setCellRenderer(new ListCell());
		}
		return jl_User;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jl_state = new JLabel();
			jl_state.setText("state");
			jLabel3 = new JLabel();
			jLabel3.setText("当前状态:");
			jl_Port = new JLabel();
			jl_Port.setText("port");
			jLabel2 = new JLabel();
			jLabel2.setText("   侦听端口:");
			jl_ServerAddr = new JLabel();
			jl_ServerAddr.setText("addr");
			jLabel1 = new JLabel();
			jLabel1.setText("         服务器地址:");
			jl_Logsum = new JLabel();
			jl_Logsum.setText("logsum");
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(0);
			flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
			flowLayout.setVgap(0);
			jLabel = new JLabel();
			jLabel.setText("   在线人数:");
			jPanel1 = new JPanel();
			jPanel1.setLayout(flowLayout);
			jPanel1.add(jLabel3, null);
			jPanel1.add(jl_state, null);
			jPanel1.add(jLabel, null);
			jPanel1.add(jl_Logsum, null);
			jPanel1.add(jLabel1, null);
			jPanel1.add(jl_ServerAddr, null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(jl_Port, null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jMenu1	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu1() {
		if (jMenu1 == null) {
			jMenu1 = new JMenu();
			jMenu1.setText("设置");
			jMenu1.add(getJm_Serverset());
			jMenu1.add(getJm_Accessset());
		}
		return jMenu1;
	}

	/**
	 * This method initializes jm_Serverset	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJm_Serverset() {
		if (jm_Serverset == null) {
			jm_Serverset = new JMenuItem("服务端配置");
			jm_Serverset.addActionListener(this);
		}
		return jm_Serverset;
	}

	/**
	 * This method initializes jm_Accessset	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJm_Accessset() {
		if (jm_Accessset == null) {
			jm_Accessset = new JMenuItem("权限设置");
		}
		return jm_Accessset;
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				QQServer thisClass = new QQServer();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	/**
	 * This is the default constructor
	 */
	public QQServer() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		dlm_User=new DefaultListModel();	
		lci=new ListCellInfo();
		lciend=new ListCellInfo();
		
		this.setSize(665, 462);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		Function.setCenter(this);
		this.setTitle("企业QQ服务端");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJJToolBarBar(), BorderLayout.NORTH);
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
			jContentPane.add(getJPanel1(), BorderLayout.SOUTH);
		}
		return jContentPane;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==jr_Start)
		{
			jr_Start.setEnabled(false);
			jr_Pause.setEnabled(true);
			jr_Stop.setEnabled(true);
			jl_state.setText("服务端正在运行...");
			this.setTitle("服务端正在运行...");
			
			rc=new ReadConfig();         //创建读取配置类
			Server_port=Integer.parseInt(rc.getString("port", false));//读取服务端口
			rc.closeRC();
			
			if(rm==null)					//创建接收线程
				rm=new ReceivMsg(Server_port,this);
			else
			{
				rm.setPort(Server_port);
			}
			
			if(sm==null)                   //创建发送线程
				sm=new SendMsg(this);
			
			sm.start();
			rm.start();
		}
		else if(e.getSource()==jr_Pause)
		{
			jr_Start.setEnabled(true);
			jr_Pause.setEnabled(false);
			jr_Stop.setEnabled(true);
			jl_state.setText("服务器暂停...");
			this.setTitle("服务器暂停...");
			
			rm.pause();
			sm.pause();
		}
		else if(e.getSource()==jr_Stop)
		{
			jr_Start.setEnabled(true);
			jr_Pause.setEnabled(false);
			jr_Stop.setEnabled(false);
			jl_state.setText("服务器停止...");
			this.setTitle("服务器停止...");
			
			rm.stop();
			sm.stop();
		}
		else if(e.getSource()==jm_Serverset)
		{
			if(ss==null)
				ss=new ServerSet();
			else
				ss.setVisible(true);
		}
	}
}

⌨️ 快捷键说明

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