- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Ch80 implements ActionListener{
- private JFrame fm1;
- private JLabel lb1, lb2;
- private JTextField tf3, tf2;
- private JButton bt1, bt2;
- Ch80(){
- fm1=new JFrame("土地面積運算");
- lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
- lb2=new JLabel("輸入坪數:");
- tf3=new JTextField();
- tf2=new JTextField();
- bt1=new JButton("確定");
- bt2=new JButton("清除");
-
- bt1.setBounds(10,135,93,25);
- bt1.addActionListener(this);
- bt2.setBounds(113,135,93,25);
- bt2.addActionListener(this);
-
- tf2.setBounds(70, 50, 135, 30);
- tf2.addActionListener(this);
- tf3.setBounds(10, 85, 194, 40);
- tf3.setEditable(false);
-
- lb1.setBounds(10, 10, 195, 30);
- lb2.setBounds(10, 50, 55, 30);
-
- fm1.setBounds(100, 100, 221, 200);
- fm1.setVisible(true);
- fm1.setResizable(false);
- fm1.setLayout(null);
- fm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm1.add(lb1);
- fm1.add(lb2);
- fm1.add(bt1);
- fm1.add(bt2);
- fm1.add(tf2);
- fm1.add(tf3);
- }
-
- public void actionPerformed(ActionEvent e){
- if(e.getSource()==tf2 || e.getSource()==bt1){
- double area=Double.parseDouble(tf2.getText())*3.3058;
- tf3.setText("面積為: "+area+" 平方公尺");
- }
- if(e.getSource()==bt2){
- tf2.setText("");
- tf3.setText("");
- }
- }
- public static void main(String[] args) {
- Ch80 app=new Ch80();
- }
- }
複製代碼 |