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

📄 serverlogingui.java

📁 JAVA实现的聊天工具,可以容纳最多10个用户 1.本系统需要JDK1.5 或更高版本的支持。 2.serverDatabase为服务器端的数据文件. 若使用现有数据,可用帐号:1, 密码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Collections;
import javax.swing.*;

public class ServerLogInGUI implements ActionListener,Runnable{
	private final static String LOGIN="logIn";
	private final static String EXIT="exit";
	
	private List<Administrator> adms;
	Administrator currentAdm;
	
	private JFrame frame;
	private JButton logInButton,exitButton;
	private JLabel idLabel,pwLabel;
	private JTextField idField;
	private JPasswordField pwField;
	//private JPanel centrePane;
	private JPanel centrePane; //centre piece in BorderLayout
	private JPanel southPane; //south piece in BorderLayout
	
	public ServerLogInGUI(){
		adms=(LinkedList<Administrator>)Server.getServerDatabase().getAdmList();//load adm-list
	}
	public void run(){
		JFrame.setDefaultLookAndFeelDecorated(true);
		frame=new JFrame("Welcome to the Chat-Server System!");
		//ServerLogInGUI contentPane=new ServerLogInGUI();
		frame.setContentPane(makePanel());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.pack();
        //frame.setMaximumSize(frame.getMinimumSize());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        //System.out.println(frame);
	}	
	
	private Container makePanel(){
		Container original=new JPanel();
		original.setLayout(new BoxLayout(original,BoxLayout.Y_AXIS));
		
		
		logInButton=new JButton("Log In");
		logInButton.setMnemonic(KeyEvent.VK_L);
		logInButton.setToolTipText("Log in this system.");
		logInButton.setActionCommand(LOGIN);
		logInButton.addActionListener(this);
		
		exitButton=new JButton("Exit");
		exitButton.setMnemonic(KeyEvent.VK_E);
		exitButton.setToolTipText("Exit this system.");
		exitButton.setActionCommand(EXIT);
		exitButton.addActionListener(this);
		
		//welcomeLabel=new JLabel("Welcome to the Chat-Server System!");
		//welcomeLabel.setHorizontalAlignment(SwingConstants.LEADING);
		
		idField=new JTextField();
		//idField.setMaximumSize(idField.getMinimumSize());
		pwField=new JPasswordField();
		pwField.setEchoChar('●');
		pwField.setActionCommand(LOGIN);
		pwField.addActionListener(this);
		
		idLabel=new JLabel("Please input your ID:");
		pwLabel=new JLabel("Please input your password:");
		
		//northPane=new JPanel(new BorderLayout());
		//northPane.add(welcomeLabel,BorderLayout.CENTER);
		centrePane=new JPanel(new GridLayout(2,2));		
		centrePane.add(idLabel);
		centrePane.add(idField);
		centrePane.add(pwLabel);
		centrePane.add(pwField);
		
		southPane=new JPanel();
		southPane.setLayout(new BoxLayout(southPane,BoxLayout.X_AXIS));
		southPane.add(logInButton);
		southPane.add(Box.createHorizontalStrut(50));
		southPane.add(exitButton);
		
		//add to main panel ,BorderLayout.NORTH ,BorderLayout.CENTER ,BorderLayout.SOUTH
		//original.add(northPane);
		original.add(centrePane);
		/*Dimension minSize = new Dimension(5, 100);
		Dimension prefSize = new Dimension(5, 100);
		Dimension maxSize = new Dimension(Short.MAX_VALUE, 100);
		original.add(new Box.Filler(minSize, prefSize, maxSize));*/
		original.add(Box.createVerticalStrut(10));
		original.add(southPane);
		return original;
	}
	
	public void actionPerformed(ActionEvent ae){
		String type=ae.getActionCommand();
		//System.out.println("test");
		if(type.equals(EXIT)){
			System.exit(1);
			//Server.logInExit();
		}
		else{ //check password (type.equals(LOGIN))
			if(!check()){
				JOptionPane.showMessageDialog(frame,"ID or Password isn't correct!",
						"Warning",JOptionPane.WARNING_MESSAGE);
				clearFields();
			}
			else{//id and pw are correct setVisible(false)
				//System.out.println(frame);
				frame.dispose();
				Server.getServer().initiate(currentAdm);
				//System.out.println("OK");
			}
		}		
	}
	
	private boolean check(){
		String password;
		Administrator a;
		int id;
		
		try{ //is id valide?
			id=Integer.parseInt(idField.getText());
		}catch(NumberFormatException e){
			return false;
		}
		password=new String(pwField.getPassword());	
		//System.out.println("id"+id);
		int index=Collections.binarySearch(adms,new Administrator(id));
		//System.out.println("index"+index+"	"+adms);
		if(index<0)
			return false;
		a=adms.get(index);
		//System.out.println("index"+index+"	"+a.getID()+a.getPassword());
		if(!password.equals(a.getPassword())){
			return false;
		}
		currentAdm=a;
		return true;
	}
	
	private void clearFields(){
		idField.selectAll();
		idField.replaceSelection(null);
		pwField.selectAll();
		pwField.replaceSelection(null);
		idField.requestFocusInWindow();
	}
}

⌨️ 快捷键说明

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