返回列表 發帖
  1. import java.awt.event.*;
  2. import javax.swing.*;

  3. public class Ch81 implements ActionListener {
  4.         JFrame fm;
  5.         JLabel lb1, lb2;
  6.         JTextField tf1, tf2;
  7.         JButton bt1, bt2;

  8.         void init() {
  9.                 lb1 = new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
  10.                 lb2 = new JLabel("輸入坪數:");
  11.                 lb1.setBounds(0, 10, 215, 30);
  12.                 lb2.setBounds(10, 40, 60, 40);

  13.                 tf1 = new JTextField();
  14.                 tf2 = new JTextField();
  15.                 tf1.setBounds(70, 45, 134, 30);
  16.                 tf2.setBounds(10, 85, 195, 40);
  17.                 tf2.setEditable(false);

  18.                 bt1 = new JButton("確定");
  19.                 bt2 = new JButton("清除");
  20.                 bt1.setBounds(10, 135, 92, 25);
  21.                 bt2.setBounds(112, 135, 92, 25);

  22.                 fm = new JFrame("土地面積換算");
  23.                 fm.setBounds(100, 100, 220, 200);
  24.                 fm.setVisible(true);
  25.                 fm.setResizable(false);
  26.                 fm.setLayout(null);
  27.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.                 fm.add(lb1);
  29.                 fm.add(lb2);
  30.                 fm.add(tf1);
  31.                 fm.add(tf2);
  32.                 fm.add(bt1);
  33.                 fm.add(bt2);

  34.         }
  35.        
  36.         void act() {
  37.                 bt1.addActionListener(this);
  38.                 bt2.addActionListener(this);
  39.                 tf1.addActionListener(this);
  40.         }

  41.         Ch81() {
  42.                 init();
  43.                 act();
  44.         }

  45.         public static void main(String[] args) {
  46.                 new Ch81();
  47.         }

  48.         public void actionPerformed(ActionEvent e) {
  49.                 if (e.getSource() == bt1 || e.getSource() == tf1) {
  50.                         Double area;
  51.                         area = Double.parseDouble(tf1.getText())*3.3058;
  52.                         tf2.setText("面積為:" + area + "平方公尺");
  53.                 }
  54.                 else if (e.getSource() == bt2) {
  55.                         tf1.setText("");
  56.                         tf2.setText("");
  57.                 }
  58.         }

  59. }
複製代碼

TOP

返回列表