📄 loginframe.java
字号:
package com.tarena.abs.client;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.tarena.abs.model.*;/** * 该类描述一个登录窗口 * @author tony.tang * */public class LoginFrame extends JFrame implements ActionListener{ private static final long serialVersionUID = 2400305667603538483L; private JLabel[] label;//所有标签 private JTextField name; private JPasswordField password; private JButton ok; private JButton cancel; private JPanel jp1,jp2,jp3; public LoginFrame(){ super("航班机票预定系统--客户登录"); label=new JLabel[3]; label[0]=new JLabel("用 户 名:"); label[1]=new JLabel("密 码:"); label[2]=new JLabel("客户登录"); name=new JTextField(20); password=new JPasswordField(20); ok=new JButton("登录"); cancel=new JButton("取消"); jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); init(); setAllFont(); addEventHandle(); } private void init(){ jp1.setLayout(new FlowLayout(FlowLayout.CENTER,10,20)); jp1.add(label[2]); this.add(jp1,BorderLayout.NORTH); jp2.setLayout(new FlowLayout(FlowLayout.CENTER,5,10)); jp2.add(label[0]);jp2.add(name); jp2.add(label[1]);jp2.add(password); this.add(jp2,BorderLayout.CENTER); jp3.setLayout(new FlowLayout(FlowLayout.CENTER,50,20)); jp3.add(ok);jp3.add(cancel); this.add(jp3,BorderLayout.SOUTH); } private void setAllFont(){ Font f1=new Font("",Font.BOLD,20); Font f2=new Font("仿宋",Font.BOLD,30); for(int i=0;i<label.length-1;i++){ label[i].setFont(f1); } label[label.length-1].setFont(f2); label[label.length-1].setForeground(Color.RED); name.setFont(f1); password.setFont(f1); ok.setFont(f1); cancel.setFont(f1); } public void showMe(){ this.setSize(540,360); this.setVisible(true); //this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void addEventHandle(){ ok.addActionListener(this); cancel.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("登录")){//如果是登录按钮 //判断文本框中的内容是否为空 if(name.getText().equals("") || new String(password.getPassword()).equals("")){ JOptionPane.showMessageDialog(this,"姓名和密码不能为空!"); return; } try { //创建一个请求对象,该请求的类型为登录请求 Request req=new Request("login"); //向请求对象中添加数据 req.setData("UserName",name.getText()); req.setData("Password",new String(password.getPassword())); //将请求对象写往服务器 ClientMainClass.oos.writeObject(req); ClientMainClass.oos.flush(); //从服务器获得一个应答对象 Response res=(Response)ClientMainClass.ois.readObject(); //解析应答对象中封装的数据 Agent user=(Agent)res.getData(); //如果返回了一个合法的代理商(登录成功) if(user!=null){ //把当前代理商对象保存成全局变量 ClientMainClass.currentUser=user; //创建客户端主界面 ClientMainClass.clientFrame=new ClientMainFrame(); //显示客户端主界面 ClientMainClass.clientFrame.showMe(); //销毁登录界面 this.dispose(); }else{//如果登录不成功 JOptionPane.showMessageDialog(this,"对不起,用户名和密码不正确,请重新输入!"); name.setText(""); password.setText(""); } } catch (Exception e1) { e1.printStackTrace(); } }else if(e.getActionCommand().equals("取消")){ try { Request req=new Request("quit"); req.setData("Name", ClientMainClass.currentUser.getName()); ClientMainClass.oos.writeObject(req); ClientMainClass.oos.flush(); System.exit(0); } catch (Exception e1) { e1.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -