- import java.awt.event.*;
- import javax.swing.*;
- public class Ch81 implements ActionListener {
- JFrame fm;
- JLabel lb1, lb2;
- JTextField tf1, tf2;
- JButton bt1, bt2;
- void init() {
- lb1 = new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
- lb2 = new JLabel("輸入坪數:");
- lb1.setBounds(0, 10, 215, 30);
- lb2.setBounds(10, 40, 60, 40);
- tf1 = new JTextField();
- tf2 = new JTextField();
- tf1.setBounds(70, 45, 134, 30);
- tf2.setBounds(10, 85, 195, 40);
- tf2.setEditable(false);
- bt1 = new JButton("確定");
- bt2 = new JButton("清除");
- bt1.setBounds(10, 135, 92, 25);
- bt2.setBounds(112, 135, 92, 25);
- fm = new JFrame("土地面積換算");
- fm.setBounds(100, 100, 220, 200);
- fm.setVisible(true);
- fm.setResizable(false);
- fm.setLayout(null);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(lb1);
- fm.add(lb2);
- fm.add(tf1);
- fm.add(tf2);
- fm.add(bt1);
- fm.add(bt2);
- }
-
- void act() {
- bt1.addActionListener(this);
- bt2.addActionListener(this);
- tf1.addActionListener(this);
- }
- Ch81() {
- init();
- act();
- }
- public static void main(String[] args) {
- new Ch81();
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == bt1 || e.getSource() == tf1) {
- Double area;
- area = Double.parseDouble(tf1.getText())*3.3058;
- tf2.setText("面積為:" + area + "平方公尺");
- }
- else if (e.getSource() == bt2) {
- tf1.setText("");
- tf2.setText("");
- }
- }
- }
複製代碼 |