返回列表 發帖
  1. package ch07;

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;

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

  8. public class Ch81 implements ActionListener{

  9.         JFrame fm;
  10.         JLabel lb1, lb2;
  11.         JTextField tf1, tf2;
  12.         JButton btn1, btn2;

  13.         void initialize()
  14.         {
  15.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  16.                 lb1.setBounds(0, 10, 215, 30);
  17.                 lb2=new JLabel("輸入坪數:");
  18.                 lb2.setBounds(10, 40, 60, 40);

  19.                 tf1=new JTextField();
  20.                 tf1.setBounds(70, 45, 134, 30);
  21.                 tf2=new JTextField();
  22.                 tf2.setBounds(10, 85, 195, 40);
  23.                 tf2.setEditable(false);
  24.                 tf1.addActionListener(this);

  25.                 btn1=new JButton("確定");
  26.                 btn2=new JButton("清除");
  27.                 btn1.setBounds(10, 135, 92, 25);
  28.                 btn2.setBounds(112, 135, 92, 25);
  29.                 btn1.addActionListener(this);
  30.                 btn2.addActionListener(this);
  31.                

  32.                 fm=new JFrame("土地面積換算");
  33.                 fm.setBounds(100, 100, 220, 200);
  34.                 fm.setVisible(true);
  35.                 fm.setResizable(false);
  36.                 fm.setLayout(null);
  37.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.                 fm.add(lb1);
  39.                 fm.add(lb2);
  40.                 fm.add(tf1);
  41.                 fm.add(tf2);
  42.                 fm.add(btn1);
  43.                 fm.add(btn2);
  44.                
  45.                
  46.         }

  47.         Ch81()  //建構子
  48.         {
  49.                 initialize();
  50.         }

  51.         public static void main(String[] args) {
  52.                 new Ch81();
  53.         }

  54.                 @Override
  55.                 public void actionPerformed(ActionEvent e) {
  56.                         if(e.getSource()==btn2)
  57.                         {
  58.                                 tf1.setText("");
  59.                              tf2.setText("");
  60.                      }
  61.                         if(e.getSource()==btn1 || e.getSource()==tf1)
  62.                         {
  63.                                 tf2.setText("面積為"+(Double.parseDouble(tf1.getText())*3.3058)+"公尺");
  64.                         }
  65.                             
  66.                 }
  67. }
複製代碼

TOP

返回列表