返回列表 發帖
  1. import javax.swing.JButton;
  2. import javax.swing.JFrame;
  3. import javax.swing.JLabel;
  4. import javax.swing.JTextField;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;

  7. public class Ch81 implements ActionListener {
  8.         JTextField t1, t2;
  9.         JLabel lb1,lb2;
  10.         JButton btn1,btn2;
  11.         JFrame fm;


  12.         void initialize(){
  13.                 t1=new JTextField();
  14.                 t2=new JTextField();
  15.                 t2.setEditable(false);

  16.                 t1.setBounds(70, 45, 134, 30);
  17.                 t2.setBounds(10, 85, 195, 40);

  18.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  19.                 lb1.setBounds(0, 10, 215, 30);
  20.                 lb2=new JLabel("輸入坪數:");
  21.                 lb2.setBounds(10, 40, 60, 40);

  22.                 btn1=new JButton("確定");
  23.                 btn1.setBounds(10, 135, 92, 25);
  24.                 btn2=new JButton("清除");
  25.                 btn2.setBounds(112, 135, 92, 25);

  26.                 fm=new JFrame("土地面積換算");
  27.                 fm.setBounds(100, 100, 220, 200);
  28.                 fm.setVisible(true);
  29.                 fm.setResizable(false);
  30.                 fm.setLayout(null);
  31.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.                 fm.add(lb1);
  33.                 fm.add(lb2);
  34.                 fm.add(t1);
  35.                 fm.add(t2);
  36.                 fm.add(btn1);
  37.                 fm.add(btn2);
  38.         }

  39.         void act(){
  40.                 btn1.addActionListener(this);
  41.                 btn2.addActionListener(this);
  42.                 t1.addActionListener(this);
  43.         }

  44.         Ch81(){
  45.                 initialize();
  46.                 act();
  47.         }

  48.         public void actionPerformed(ActionEvent e)
  49.         {
  50.                 if(e.getSource()==t1 || e.getSource()==btn1)
  51.                 {
  52.                         double area=Double.parseDouble(t1.getText())*3.3058;
  53.                         String areaStr=String.valueOf(area);
  54.                         t2.setText("面積為: "+areaStr+" 平方公尺");
  55.                 }
  56.                 if(e.getSource()==btn2)
  57.                 {
  58.                         t1.setText("");
  59.                         t2.setText("");
  60.                 }
  61.         }

  62.         public static void main(String[] args) {
  63.                 // TODO 自動產生的方法 Stub
  64.                 Ch81 app = new Ch81();
  65.         }

  66. }
複製代碼

TOP

返回列表