Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- public class De5 extends Frame implements ActionListener {
- private Label lbForm, lbName, lbPass;
- private TextField tfName, tfPass;
- private Button btLogin, btCancel;
- private GridBagLayout gb;
- private GridBagConstraints gbc;
- public De5(String title) {
- super(title);
- this.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- lbForm = new Label("Form Test");
- lbName = new Label("Name:");
- lbPass = new Label("Password:");
- tfName = new TextField(20);
- tfPass = new TextField(20);
- tfPass.setEchoChar('*');
- btLogin = new Button("Login");
- btCancel = new Button("Cancel");
- gb = new GridBagLayout();
- this.setLayout(gb);
- Them(lbForm,0,0,3,1);
- Them(lbName,0,1,1,1);
- Them(tfName,1,1,2,1);
- Them(lbPass,0,2,1,1);
- Them(tfPass,1,2,2,1);
- Them(btLogin,1,3,1,1);
- Them(btCancel,2,3,1,1);
- btLogin.addActionListener(this);
- btCancel.addActionListener(this);
- }
- public void Them(Component comp, int x, int y, int rong, int cao) {
- gbc = new GridBagConstraints();
- gbc.gridx = x;
- gbc.gridy = y;
- gbc.gridwidth = rong;
- gbc.gridheight = cao;
- gb.setConstraints(comp, gbc);
- this.add(comp);
- }
- public void actionPerformed(ActionEvent ae) {
- File file = new File("login.txt");
- if (ae.getSource() == btLogin) {
- DocFile(file);
- }
- if (ae.getSource() == btCancel) {
- tfName.setText("");
- tfPass.setText("");
- }
- }
- public void DocFile(File f) {
- try {
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- StringBuilder sb = new StringBuilder();
- String strUser = br.readLine();
- sb.append("\n");
- String strPass = br.readLine();
- boolean blUser = strUser.equals(tfName.getText());
- boolean blPass = strPass.equals(tfPass.getText());
- if ((blUser == true) && (blPass == true)) {
- System.out.println ("Dang nhap thanh cong");
- } else {
- System.out.println ("Dang nhap that bai");
- }
- br.close();
- fr.close();
- }
- catch (Exception ex) {
- System.out.println ("Loi roi!");
- }
- }
- public static void main(String[] args) {
- De5 fr = new De5("De 5 - Form Test");
- fr.setSize(300,200);
- fr.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment