Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- public class De3 extends Frame implements ActionListener {
- private Label lbForm, lbName, lbSex, lbAddress;
- private TextField tfName, tfAddress;
- private Button btRegis, btCancel;
- private CheckboxGroup cbgSex;
- private Checkbox cbMale, cbFemale;
- private GridBagLayout gb;
- private GridBagConstraints gbc;
- public De3(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:");
- lbAddress = new Label("Address:");
- lbSex = new Label("Sex:");
- tfName = new TextField(20);
- tfAddress = new TextField(20);
- btRegis = new Button("Regis");
- btCancel = new Button("Cancel");
- cbgSex = new CheckboxGroup();
- cbMale = new Checkbox("Male",cbgSex,false);
- cbFemale = new Checkbox("Female",cbgSex,false);
- 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(lbSex,0,2,1,1);
- Them(cbMale,1,2,1,1);
- Them(cbFemale,2,2,1,1);
- Them(lbAddress,0,3,1,1);
- Them(tfAddress,1,3,2,1);
- Them(btRegis,1,4,1,1);
- Them(btCancel,2,4,1,1);
- btRegis.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) {
- if (ae.getSource() == btRegis) {
- System.out.println ("Name: " + tfName.getText());
- if (cbMale.getState()) {
- System.out.println ("Sex: Male");
- }
- if (cbFemale.getState()) {
- System.out.println ("Sex: Female");
- }
- System.out.println ("Address: " + tfAddress.getText());
- }
- if (ae.getSource() == btCancel) {
- System.exit(0);
- }
- }
- public static void main(String[] args) {
- De3 fr = new De3("De 3 - Form Test");
- fr.setSize(300,200);
- fr.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment