thieumao

Đề 3 KMA

Apr 20th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. public class De3 extends Frame implements ActionListener {
  5.    
  6.     private Label lbForm, lbName, lbSex, lbAddress;
  7.     private TextField tfName, tfAddress;
  8.     private Button btRegis, btCancel;
  9.     private CheckboxGroup cbgSex;
  10.     private Checkbox cbMale, cbFemale;
  11.     private GridBagLayout gb;
  12.     private GridBagConstraints gbc;
  13.  
  14.     public De3(String title) {
  15.         super(title);
  16.         this.addWindowListener(new WindowAdapter() {
  17.             public void windowClosing(WindowEvent we) {
  18.                 System.exit(0);
  19.             }
  20.         });
  21.        
  22.         lbForm = new Label("Form Test");
  23.         lbName = new Label("Name:");
  24.         lbAddress = new Label("Address:");
  25.         lbSex = new Label("Sex:");
  26.         tfName = new TextField(20);
  27.         tfAddress = new TextField(20);
  28.         btRegis = new Button("Regis");
  29.         btCancel = new Button("Cancel");
  30.         cbgSex = new CheckboxGroup();
  31.         cbMale = new Checkbox("Male",cbgSex,false);
  32.         cbFemale = new Checkbox("Female",cbgSex,false);
  33.        
  34.         gb = new GridBagLayout();
  35.         this.setLayout(gb);
  36.         Them(lbForm,0,0,3,1);
  37.         Them(lbName,0,1,1,1);
  38.         Them(tfName,1,1,2,1);
  39.         Them(lbSex,0,2,1,1);
  40.         Them(cbMale,1,2,1,1);
  41.         Them(cbFemale,2,2,1,1);
  42.         Them(lbAddress,0,3,1,1);
  43.         Them(tfAddress,1,3,2,1);
  44.         Them(btRegis,1,4,1,1);
  45.         Them(btCancel,2,4,1,1);
  46.        
  47.         btRegis.addActionListener(this);
  48.         btCancel.addActionListener(this);
  49.     }
  50.    
  51.     public void Them(Component comp, int x, int y, int rong, int cao) {
  52.         gbc = new GridBagConstraints();
  53.         gbc.gridx = x;
  54.         gbc.gridy = y;
  55.         gbc.gridwidth = rong;
  56.         gbc.gridheight = cao;
  57.         gb.setConstraints(comp, gbc);
  58.         this.add(comp);
  59.     }
  60.    
  61.     public void actionPerformed(ActionEvent ae) {
  62.         if (ae.getSource() == btRegis) {
  63.             System.out.println ("Name: " + tfName.getText());
  64.             if (cbMale.getState()) {
  65.                 System.out.println ("Sex: Male");
  66.             }
  67.             if (cbFemale.getState()) {
  68.                 System.out.println ("Sex: Female");
  69.             }
  70.             System.out.println ("Address: " + tfAddress.getText());
  71.         }
  72.         if  (ae.getSource() == btCancel) {
  73.             System.exit(0);
  74.         }
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         De3 fr = new De3("De 3 - Form Test");
  79.         fr.setSize(300,200);
  80.         fr.setVisible(true);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment