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 De6 extends Frame implements ActionListener {
- private Label lbForm, lbUser, lbPass;
- private TextField tfUser, tfPass;
- private Button btRegister, btCancel;
- private GridBagLayout gb;
- private GridBagConstraints gbc;
- public De6(String title) {
- super(title);
- this.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent we) {
- System.exit(0);
- }
- });
- lbForm = new Label("Form Test");
- lbUser = new Label("Name:");
- lbPass = new Label("Password:");
- tfUser = new TextField(20);
- tfPass = new TextField(20);
- tfPass.setEchoChar('*');
- btRegister = new Button("Register");
- btCancel = new Button("Cancel");
- gb = new GridBagLayout();
- this.setLayout(gb);
- Them(lbForm,1,0,2,1);
- Them(lbUser,0,1,1,1);
- Them(tfUser,1,1,2,1);
- Them(lbPass,0,2,1,1);
- Them(tfPass,1,2,2,1);
- Them(btRegister,1,3,1,1);
- Them(btCancel,2,3,1,1);
- btRegister.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 GhiFile(File f) {
- try {
- FileWriter fw = new FileWriter(f);
- BufferedWriter bw = new BufferedWriter(fw);
- bw.write(tfUser.getText());
- bw.write("\n");
- bw.write(tfPass.getText());
- bw.close();
- fw.close();
- }
- catch (Exception ex) {
- System.out.println ("Loi roi");
- }
- }
- public void actionPerformed(ActionEvent ae) {
- if (ae.getSource() == btRegister) {
- File file = new File("login.txt");
- GhiFile(file);
- }
- if (ae.getSource() == btCancel) {
- tfUser.setText("");
- tfPass.setText("");
- }
- }
- public static void main(String[] args) {
- De6 fr = new De6("De 6 - Form Test");
- fr.setSize(300,200);
- fr.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment