thieumao

Đề 5 KMA

Apr 21st, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4.  
  5. public class De5 extends Frame implements ActionListener {
  6.    
  7.     private Label lbForm, lbName, lbPass;
  8.     private TextField tfName, tfPass;
  9.     private Button btLogin, btCancel;
  10.     private GridBagLayout gb;
  11.     private GridBagConstraints gbc;
  12.        
  13.     public De5(String title) {
  14.         super(title);
  15.         this.addWindowListener(new WindowAdapter() {
  16.             public void windowClosing(WindowEvent we) {
  17.                 System.exit(0);
  18.             }
  19.         });
  20.         lbForm = new Label("Form Test");
  21.         lbName = new Label("Name:");
  22.         lbPass = new Label("Password:");
  23.         tfName = new TextField(20);
  24.         tfPass = new TextField(20);
  25.         tfPass.setEchoChar('*');
  26.         btLogin = new Button("Login");
  27.         btCancel = new Button("Cancel");
  28.        
  29.         gb = new GridBagLayout();
  30.         this.setLayout(gb);
  31.         Them(lbForm,0,0,3,1);
  32.         Them(lbName,0,1,1,1);
  33.         Them(tfName,1,1,2,1);
  34.         Them(lbPass,0,2,1,1);
  35.         Them(tfPass,1,2,2,1);
  36.         Them(btLogin,1,3,1,1);
  37.         Them(btCancel,2,3,1,1);
  38.        
  39.         btLogin.addActionListener(this);
  40.         btCancel.addActionListener(this);
  41.     }
  42.    
  43.     public void Them(Component comp, int x, int y, int rong, int cao) {
  44.         gbc = new GridBagConstraints();
  45.         gbc.gridx = x;
  46.         gbc.gridy = y;
  47.         gbc.gridwidth = rong;
  48.         gbc.gridheight = cao;
  49.         gb.setConstraints(comp, gbc);
  50.         this.add(comp);
  51.     }
  52.    
  53.     public void actionPerformed(ActionEvent ae) {
  54.         File file = new File("login.txt");
  55.         if (ae.getSource() == btLogin) {
  56.             DocFile(file);
  57.         }
  58.         if (ae.getSource() == btCancel) {
  59.             tfName.setText("");
  60.             tfPass.setText("");
  61.         }
  62.     }
  63.    
  64.     public void DocFile(File f) {
  65.         try {
  66.             FileReader fr = new FileReader(f);
  67.             BufferedReader br = new BufferedReader(fr);
  68.             StringBuilder sb = new StringBuilder();
  69.             String strUser = br.readLine();
  70.             sb.append("\n");
  71.             String strPass = br.readLine();
  72.             boolean blUser = strUser.equals(tfName.getText());
  73.             boolean blPass = strPass.equals(tfPass.getText());
  74.             if ((blUser == true) && (blPass == true)) {
  75.                 System.out.println ("Dang nhap thanh cong");
  76.             } else {
  77.                 System.out.println ("Dang nhap that bai");
  78.             }
  79.             br.close();
  80.             fr.close();
  81.         }
  82.         catch (Exception ex) {
  83.             System.out.println ("Loi roi!");
  84.         }
  85.     }
  86.    
  87.     public static void main(String[] args) {
  88.         De5 fr = new De5("De 5 - Form Test");
  89.         fr.setSize(300,200);
  90.         fr.setVisible(true);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment