返回列表 發帖
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;

  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTextField;


  7. public class Ch80 implements ActionListener
  8. {
  9.     JFrame fm;
  10.     JLabel lb1,lb2;
  11.     JTextField tf1,tf2;
  12.     JButton btn1,btn2;   
  13.         Ch80()
  14.     {
  15.              fm = new JFrame("土地面積計算");
  16.          lb1 = new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  17.          lb2 = new JLabel("輸入坪數:");
  18.          tf1 = new JTextField();
  19.          tf2 = new JTextField();
  20.          btn1 = new JButton("確定");
  21.          btn2 = new JButton("清除");
  22.          
  23.          lb1.setBounds(0, 10, 215, 30);
  24.          lb2.setBounds(10, 40, 60, 40);
  25.          tf1.setBounds(70,45,134,30);
  26.          tf2.setBounds(10,85,195,40);
  27.          btn1.setBounds(10,135,92,25);
  28.          btn2.setBounds(112,135,92,25);
  29.          fm.setBounds(100, 100, 220, 200);
  30.          
  31.          btn2.addActionListener(this);
  32.          btn1.addActionListener(this);
  33.          tf1.addActionListener(this);
  34.          tf2.setEnabled(false);
  35.          
  36.          fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.          fm.setVisible(true);
  38.          fm.setResizable(true);
  39.          fm.setLayout(null);
  40.          fm.add(btn1);
  41.          fm.add(btn2);
  42.          fm.add(lb1);
  43.          fm.add(lb2);
  44.          fm.add(tf1);
  45.          fm.add(tf2);
  46.     }
  47.        
  48.         public static void main(String[] args)
  49.         {
  50.             Ch80 app=new Ch80();        
  51.         }

  52.                 public void actionPerformed(ActionEvent e)
  53.                 {
  54.                         if(e.getSource()==tf1 || e.getSource()==btn1)
  55.                         {
  56.                                 double area = Double.parseDouble(tf1.getText())*3.3058;
  57.                                 tf2.setText("面積為"+area+"公尺");
  58.                         }
  59.                         if(e.getSource()==btn2)
  60.                         {
  61.                                 tf1.setText("");
  62.                                 tf2.setText("");
  63.                         }
  64.                 }

  65. }
複製代碼

TOP

返回列表