thieumao

Đề 6 KMA

Apr 22nd, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4.  
  5. public class De6 extends Frame implements ActionListener {
  6.    
  7.     private Label lbForm, lbUser, lbPass;
  8.     private TextField tfUser, tfPass;
  9.     private Button btRegister, btCancel;
  10.     private GridBagLayout gb;
  11.     private GridBagConstraints gbc;
  12.        
  13.     public De6(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.         lbUser = new Label("Name:");
  22.         lbPass = new Label("Password:");
  23.         tfUser = new TextField(20);
  24.         tfPass = new TextField(20);
  25.         tfPass.setEchoChar('*');
  26.         btRegister = new Button("Register");
  27.         btCancel = new Button("Cancel");
  28.        
  29.         gb = new GridBagLayout();
  30.         this.setLayout(gb);
  31.         Them(lbForm,1,0,2,1);
  32.         Them(lbUser,0,1,1,1);
  33.         Them(tfUser,1,1,2,1);
  34.         Them(lbPass,0,2,1,1);
  35.         Them(tfPass,1,2,2,1);
  36.         Them(btRegister,1,3,1,1);
  37.         Them(btCancel,2,3,1,1);
  38.        
  39.         btRegister.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 GhiFile(File f) {
  54.         try {
  55.             FileWriter fw = new FileWriter(f);
  56.             BufferedWriter bw = new BufferedWriter(fw);
  57.             bw.write(tfUser.getText());
  58.             bw.write("\n");
  59.             bw.write(tfPass.getText());
  60.             bw.close();
  61.             fw.close();
  62.         }
  63.         catch (Exception ex) {
  64.             System.out.println ("Loi roi");
  65.         }
  66.     }
  67.    
  68.     public void actionPerformed(ActionEvent ae) {
  69.         if (ae.getSource() == btRegister) {
  70.             File file = new File("login.txt");
  71.             GhiFile(file);
  72.         }
  73.         if (ae.getSource() == btCancel) {
  74.             tfUser.setText("");
  75.             tfPass.setText("");
  76.         }
  77.     }
  78.    
  79.     public static void main(String[] args) {
  80.         De6 fr = new De6("De 6 - Form Test");
  81.         fr.setSize(300,200);
  82.         fr.setVisible(true);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment