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

📄 login.java

📁 聊天系统。1、聊天 2、传文件 3、多人聊天
💻 JAVA
字号:

package com.client;

import java.net.*;
import java.io.*;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JComboBox ;
import javax.swing.JOptionPane ;

/**
 * @author gt.Cloud
 * 
 * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class Login extends JFrame implements ActionListener {
	JTextField t_username = new JTextField();

	JPasswordField t_password = new JPasswordField();
	JComboBox c_author = new JComboBox() ;
	
	Login() {
		//设置窗体属性
		this.setSize(250, 150);
		this.setResizable(false);

		int width = (int) Toolkit.getDefaultToolkit().getScreenSize()
				.getWidth();
		int height = (int) Toolkit.getDefaultToolkit().getScreenSize()
				.getHeight();
		this.setLocation((width - 250) / 2, (height - 150) / 2);

		//new一大堆组件
		JLabel l_username = new JLabel("用户名");
		JLabel l_password = new JLabel("密码");

		
		JButton b_ok = new JButton("登录");
		JButton b_cancel = new JButton("取消");
//		JButton b_reg = new JButton("注册");
		
		c_author.addItem("学生") ;
		c_author.addItem("教师") ;
		c_author.addItem("管理员") ;

		//注册事件
		b_ok.addActionListener(this);
		b_cancel.addActionListener(this);
//		b_reg.addActionListener(this);

		//布置输入面板
		JPanel p_input = new JPanel();
		p_input.setLayout(new GridLayout(2, 2));

		p_input.add(l_username);
		p_input.add(t_username);

		p_input.add(l_password);
		p_input.add(t_password);

		//布置按钮面板
		JPanel p_button = new JPanel();
		p_button.setLayout(new FlowLayout());
		
		p_button.add(c_author) ;
		p_button.add(b_ok);
//		p_button.add(b_reg);
		p_button.add(b_cancel);

		//布置窗体
		this.setLayout(new BorderLayout());

		this.add(p_input, BorderLayout.CENTER);
		this.add(p_button, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		Login w = new Login();
		w.setVisible(true);
	}

	/*
	 * (非 Javadoc)
	 * 
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand().equals("取消")) {
			System.exit(0);
		}
		if (arg0.getActionCommand().equals("登录")) {
			//到服务器验证
			//(2)
			//将用户名&密码发送到服务器
			try {
				Socket s = new Socket("127.0.0.1" , 8000) ;
				
				OutputStream os = s.getOutputStream() ;
				OutputStreamWriter osw = new OutputStreamWriter(os) ;
				PrintWriter pw = new PrintWriter(osw , true) ;
				
				pw.println(t_username.getText()+"%"+t_password.getText()+"%"+c_author.getSelectedItem()) ;
				
				//(6)
				//等待接收服务器发送回来的信息
				InputStreamReader isr = new InputStreamReader(s.getInputStream()) ;
				BufferedReader br = new BufferedReader(isr) ;
				
				String yorn = br.readLine() ;
				
				
				//(8)
				//弹出窗体
				if (yorn.equals("OK")) {
					//显示主窗体
					String who = (String)c_author.getSelectedItem() ;
					System.out.println (who+t_username.getText()+"登录成功!") ;
					Main w = null;
					if(who.equals("学生")){
						w = new Main( false , false);	
					}
					if(who.equals("教师")){
						w = new Main( true , false);
					}
					if(who.endsWith("管理员")){
						w = new Main( true , true);
					}
					w.setSocket(s , t_username.getText()) ;
					w.setVisible(true);
					this.setVisible(false);
				} else {
					JOptionPane op = new JOptionPane() ;
					op.showMessageDialog(this,"用户名或密码错误") ;
					t_username.setText("");
					t_password.setText("");
				}
			} catch (Exception ee) {
			}
		}
/*		if (arg0.getActionCommand().equals("注册")) {
			Reg w = new Reg(this);
			w.setVisible(true);
			this.setVisible(false);

		}
*/
	}
}

⌨️ 快捷键说明

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