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

📄 servergui.java

📁 chat room
💻 JAVA
字号:
package Server;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import ShareData.Message;

//import ShareData.Message;

public class ServerGUI extends JFrame{

	private static final long serialVersionUID = 1L;

	private JMenuBar menuBar=new JMenuBar();

	private JMenu menuC=new JMenu("Setting");
	private JMenu menuH=new JMenu("Help");
	
	private JMenuItem menuIC=new JMenuItem("Connect",KeyEvent.VK_C);
	private JMenuItem menuIQ=new JMenuItem("Quit",KeyEvent.VK_Q);
	
	private JTextArea recTxt=new JTextArea();
	private JTextArea sendTxt=new JTextArea(3,25);
	private JScrollPane jsp=new JScrollPane(sendTxt);
	private JScrollPane recjsp=new JScrollPane(recTxt);
	
	private JButton sendBtn=new JButton(" Send ");
	private JButton cancelBtn=new JButton("Cancel");
	
	private JPanel pane1=new JPanel();
	private JPanel pane2=new JPanel();
	
	private JList clientList=new JList();
	private JScrollPane jspUser=new JScrollPane(clientList);
	
	private IServer is;
	
	
	public void iniMenu(){
		setJMenuBar(menuBar);		
		menuBar.add(menuC);
		menuBar.add(menuH);
		
		menuC.add(menuIC);
		menuC.add(menuIQ);	
		
	}
	
	
	public void iniPane(){
		
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		pane1.setLayout(new BoxLayout(pane1,BoxLayout.Y_AXIS));
		pane1.add(sendBtn);
		pane1.add(cancelBtn);
		
		pane2.add(jsp,FlowLayout.LEFT);
		pane2.add(pane1);
		pane2.setBackground(Color.LIGHT_GRAY);

		add(pane2,BorderLayout.PAGE_END);
		
		
	}
	
	public void addEvent(){
		menuIC.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				is.start();
			}
			
		});
		
		menuIQ.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				is.close();
			}
			
		});	
		
		cancelBtn.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				sendTxt.setText("");
			}
			
		});
		
		sendBtn.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(clientList.isSelectionEmpty())
					is.sendToAll(new Message("conversation","--sever--"+sendTxt.getText()));
				else{
					RemoteClient rc=is.getRemoteClientbyName(clientList.getSelectedValue().toString());
					is.sendToOne(new Message("conversation","--sever--"+sendTxt.getText()), rc);
					
				}
				
			}
			
		}); 
		
	}
	
	public void updateShowMessage(String message){
		recTxt.append(message+"\r\n");
	}
	
	public void updateClientList(Object[] l){
		clientList.setListData(l);
	}
	public ServerGUI(IServer is){
		this.is=is;
		setBackground(Color.CYAN);
		iniMenu();
		iniPane();
		add(recjsp);
//		recTxt.setBackground(Color.YELLOW);
		add(jspUser,BorderLayout.LINE_END);
		clientList.setFixedCellWidth(85);
		addEvent();
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setTitle("Chatting_ServerGUI");
		
		this.setSize(400, 300);
//		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
}

⌨️ 快捷键说明

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