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

📄 loginframe.java

📁 老师给的在线航班系统
💻 JAVA
字号:
package com.toa.abs.cui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;

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.SwingConstants;

import com.toa.abs.model.Request;
import com.toa.abs.model.Response;
import com.toa.abs.model.Sales;

public class LoginFrame extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField1 = null;
	private JPanel jPanel2 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;
	private JPasswordField jPasswordField = null;
	/**
	 * This is the default constructor
	 */
	public LoginFrame() {
		super();
		initialize();
		addEventHandler();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(290, 218);
		this.setLocation(new Point(400, 300));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJPanel());
		this.setTitle("TOA国内机票售票系统-营业员登录");
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setText("营业员登录");
			jLabel.setFont(new Font("\u96b6\u4e66", Font.BOLD, 24));
			jLabel.setForeground(new Color(0, 0, 204));
			jLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(jLabel, BorderLayout.NORTH);
			jPanel.add(getJPanel2(), BorderLayout.SOUTH);
			jPanel.add(getJPanel1(), BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setVgap(15);
			flowLayout1.setHgap(20);
			jLabel2 = new JLabel();
			jLabel2.setText("密码:");
			jLabel1 = new JLabel();
			jLabel1.setText("帐号:");
			jLabel1.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel1.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			jPanel1 = new JPanel();
			jPanel1.setLayout(flowLayout1);
			jPanel1.add(jLabel1, null);
			jPanel1.add(getJTextField1(), null);
			jPanel1.add(jLabel2, null);
			jPanel1.add(getJPasswordField(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(150, 22));
			jTextField1.setFont(new Font("Dialog", Font.BOLD, 14));
		}
		return jTextField1;
	}
	
	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(20);
			jPanel2 = new JPanel();
			jPanel2.setLayout(flowLayout);
			jPanel2.add(getJButton2(), null);
			jPanel2.add(getJButton3(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("登录");
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText("取消");
		}
		return jButton3;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setPreferredSize(new Dimension(150, 22));
			jPasswordField.setFont(new Font("Dialog", Font.BOLD, 14));
		}
		return jPasswordField;
	}
	
	
	public void addEventHandler(){
		jButton2.addActionListener(this);
		jButton3.addActionListener(this);
	}
	
	

	public void actionPerformed(ActionEvent e) {
		String str=e.getActionCommand();
		if(str.equals("登录")){
			//判断id和密码是否有效!
			//封装一个请求对象,类型:LOGIN_REQ
			//封装请求参数:UserName,Password
			//发送请求到服务器
			//得到服务器的应答
			//分析应答中包含的数据
			//如果登录成功,则打开主界面,否则弹出错误提示。
			String name=jTextField1.getText().trim();
			String passwd=new String(jPasswordField.getPassword()).trim();
			if(name.equals("")||passwd.equals("")){
				ClientMainClass.config.getErrorUtil().showErrorMessageDialog(1009);
				return;
			}
			Request req=new Request(Request.LOGIN_REQ);
			req.addParameter("UserName", name);
			req.addParameter("Password", passwd);
			
			try {
				ClientMainClass.config.getOos().writeObject(req);
				ClientMainClass.config.getOos().flush();
				
				Response res=(Response)ClientMainClass.config.getOis().readObject();
				Sales s=(Sales)res.getValue("Sale");
				if(s==null){
					ClientMainClass.config.getErrorUtil().showErrorMessageDialog(1010);
					ClientMainClass.config.getLogUtil().printMessage(name+",登录失败,无效的帐号和密码");
				}else{
					ClientMainClass.config.getLogUtil().printMessage(name+",登录成功");
					JFrame f=new ClientMainFrame();
					f.setTitle(f.getTitle()+"--"+s);
					f.setVisible(true);
					this.dispose();
				}
			} catch(ClassNotFoundException e3){
				ClientMainClass.config.getErrorUtil().showErrorMessageDialog(1008);
				return;
			}catch (IOException e1) {
				ClientMainClass.config.getErrorUtil().showErrorMessageDialog(1007);
				return;
			}
			
			
			
		}else if(str.equals("取消")){
			//????
		}
	}

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

}  //  @jve:decl-index=0:visual-constraint="53,25"

⌨️ 快捷键说明

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