thieumao

Đề 2 KMA

Apr 20th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class De2 extends Frame implements ActionListener {
  6.  
  7.     private Label lbForm, lbNguon, lbDich;
  8.     private TextField tfNguon, tfDich;
  9.     private Button btCopy, btHuy;
  10.     private GridBagLayout gb;
  11.     private GridBagConstraints gbc;
  12.  
  13.     public De2(String title) {
  14.         super(title);
  15.         this.addWindowListener(new WindowAdapter() {
  16.             public void windowClosing(WindowEvent we) {
  17.                 System.exit(0);
  18.             }
  19.         });
  20.  
  21.         lbForm = new Label("Form Test");
  22.         lbNguon = new Label("Source:");
  23.         lbDich = new Label("Destination:");
  24.         tfNguon = new TextField(20);
  25.         tfDich = new TextField(20);
  26.         btCopy = new Button("Copy to");
  27.         btHuy = new Button("Cancel");
  28.  
  29.         gb = new GridBagLayout();
  30.         this.setLayout(gb);
  31.  
  32.         this.Them(lbForm, 0, 0, 2, 1);
  33.         this.Them(lbNguon, 0, 1, 1, 1);
  34.         this.Them(lbDich, 0, 2, 1, 1);
  35.         this.Them(tfNguon, 1, 1, 1, 1);
  36.         this.Them(tfDich, 1, 2, 1, 1);
  37.         this.Them(btCopy, 0, 3, 1, 1);
  38.         this.Them(btHuy, 1, 3, 1, 1);
  39.  
  40.         btCopy.addActionListener(this);
  41.         btHuy.addActionListener(this);
  42.     }
  43.  
  44.     public void Them(Component comp, int x, int y, int rong, int cao) {
  45.         gbc = new GridBagConstraints();
  46.         gbc.gridx = x;
  47.         gbc.gridy = y;
  48.         gbc.gridwidth = rong;
  49.         gbc.gridheight = cao;
  50.         gb.setConstraints(comp, gbc);
  51.         this.add(comp);
  52.     }
  53.  
  54.     public void actionPerformed(ActionEvent ae) {
  55.         if (ae.getSource() == btCopy ) {
  56.             String str = tfNguon.getText();
  57.             if (str.isEmpty()) {
  58.                 System.out.println ("Loi roi");
  59.             } else {
  60.                 tfDich.setText(str);
  61.             }
  62.         }
  63.         if (ae.getSource() == btHuy) {
  64.             tfNguon.getText();
  65.             tfDich.getText();
  66.             tfNguon.setText("");
  67.             tfDich.setText("");
  68.         }
  69.     }
  70.  
  71.     public static void main(String[] args) {
  72.         De2 fr = new De2("De 2 - Form Test");
  73.         fr.setSize(300, 200);
  74.         fr.setVisible(true);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment