thieumao

De 7 - Tinh Luong

Jun 7th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package javaapplication;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class De7 extends Frame implements ActionListener {
  7.  
  8.     Label lbFormTest, lbBasicSalary, lbRate, lbSalary;
  9.     TextField tfBasicSalary, tfRate, tfSalary;
  10.     Button btCalculate;
  11.     GridBagLayout gb;
  12.     GridBagConstraints gbc;
  13.  
  14.     public De7(String title) {
  15.         super(title);
  16.  
  17.         this.addWindowListener(new WindowAdapter() {
  18.             public void windowClosing(WindowEvent we) {
  19.                 System.exit(0);
  20.             }
  21.         });
  22.  
  23.         lbFormTest = new Label("FormTest");
  24.         lbBasicSalary = new Label("BasicSalary");
  25.         lbRate = new Label("Rate");
  26.         lbSalary = new Label("Salary");
  27.         tfBasicSalary = new TextField(20);
  28.         tfRate = new TextField(20);
  29.         tfSalary = new TextField(20);
  30.         btCalculate = new Button("Calculate");
  31.  
  32.         tfSalary.setEnabled(false);
  33.         gb = new GridBagLayout();
  34.         this.setLayout(gb);
  35.         this.Them(lbFormTest, 1, 0, 1, 1);
  36.         this.Them(lbBasicSalary, 0, 1, 1, 1);
  37.         this.Them(lbRate, 0, 2, 1, 1);
  38.         this.Them(lbSalary, 0, 3, 1, 1);
  39.         this.Them(tfBasicSalary, 1, 1, 1, 1);
  40.         this.Them(tfRate, 1, 2, 1, 1);
  41.         this.Them(tfSalary, 1, 3, 1, 1);
  42.         this.Them(btCalculate, 1, 4, 1, 1);
  43.         btCalculate.addActionListener(this);
  44.     }
  45.  
  46.     public void Them(Component comp, int x, int y, int rong, int cao) {
  47.         gbc = new GridBagConstraints();
  48.         gbc.gridx = x;
  49.         gbc.gridy = y;
  50.         gbc.gridwidth = rong;
  51.         gbc.gridheight = cao;
  52.         gb.setConstraints(comp, gbc);
  53.         this.add(comp);
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.         De7 fr = new De7("Thieu Mao");
  58.         fr.setSize(400, 300);
  59.         fr.setVisible(true);
  60.     }
  61.  
  62.     public void actionPerformed(ActionEvent a) {
  63.         if(a.getSource()==btCalculate){
  64.             if(tfBasicSalary.getText().isEmpty()){
  65.                 System.out.println("Loi");
  66.             }else{
  67.                 if(tfRate.getText().isEmpty()){
  68.                     try{
  69.                         float bs = Float.valueOf(tfBasicSalary.getText());
  70.                         float salary = (float) (bs*2.34);
  71.                         tfSalary.setText(String.valueOf(salary));
  72.                     }catch(Exception e){
  73.                         System.out.println("Da Loi");
  74.                     }
  75.                 }else{
  76.                     try {
  77.                     float bs = Float.valueOf(tfBasicSalary.getText());
  78.                     float rate = Float.valueOf(tfRate.getText());
  79.                     float salary = (float) (rate*bs);
  80.                     tfSalary.setText(String.valueOf(salary));
  81.                     } catch (Exception e) {
  82.                         System.out.println("Da Loi Roi");  
  83.                     }
  84.                 }
  85.             }
  86.            
  87.         }
  88.        
  89.        
  90.     }
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment