返回列表 發帖
  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 Ch006  implements ActionListener{
  8.         JFrame fm;
  9.         JTextField tf1, tf2;
  10.         JLabel lb1, lb2;
  11.         JButton btn1, btn2;

  12.         Ch006() //建構子
  13.         {
  14.                 tf1=new JTextField();
  15.                 tf2=new JTextField();
  16.                 lb1=new JLabel("1坪=3.3085平方公尺", JLabel.CENTER);
  17.                 lb2=new JLabel("輸入坪數:");
  18.                 fm=new JFrame("土地面積換算");
  19.                 btn1=new JButton("確定");
  20.                 btn2=new JButton("清除");


  21.                 tf1.setBounds(70, 45, 134, 30);
  22.                 tf1.addActionListener(this);
  23.                 tf2.setBounds(10, 85, 195, 40);
  24.                 tf2.setEnabled(false);

  25.                 lb1.setBounds(0, 10, 215, 30);
  26.                 lb2.setBounds(10, 40, 60, 40);

  27.                 btn1.setBounds(10, 135, 92, 25);
  28.                 btn1.addActionListener(this);
  29.                 btn2.setBounds(112, 135, 92, 25);
  30.                 btn2.addActionListener(this);

  31.                 fm.setBounds(100, 100, 220, 200);
  32.                 fm.setVisible(true);
  33.                 fm.setResizable(false);
  34.                 fm.setLayout(null);
  35.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.                 fm.add(lb1);
  37.                 fm.add(lb2);
  38.                 fm.add(tf1);
  39.                 fm.add(tf2);
  40.                 fm.add(btn1);
  41.                 fm.add(btn2);


  42.         }

  43.         public static void main(String[] args) {
  44.                 Ch006 app=new Ch006();  //物件導向化(匿名物件)
  45.         }

  46.         @Override
  47.         public void actionPerformed(ActionEvent e) {
  48.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  49.                 {
  50.                         double res=Double.parseDouble(tf1.getText())*3.3058;
  51.                         tf2.setText("面積為: "+res+" 平方公尺");
  52.                 }
  53.                 if(e.getSource()==btn2)
  54.                 {
  55.                         tf1.setText("");
  56.                         tf2.setText("");
  57.                 }
  58.         }

  59. }
複製代碼

TOP

返回列表