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