- 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{
-
- private JFrame fm=new JFrame("土筆面積換算");
- private JLabel lb1=new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
- private JLabel lb2=new JLabel("輸入坪數:");
- private JTextField tf1=new JTextField();
- private JTextField tf2=new JTextField();
- private JButton btn1=new JButton("確定");
- private JButton btn2=new JButton("清除");
-
- Ch01(){
-
- lb1.setBounds(0, 10, 215, 30);
- lb2.setBounds(10, 40, 60, 40);
-
- tf1.setBounds(70, 45, 134, 30);
- tf1.addActionListener(this);
- tf2.setBounds(10, 85, 195, 40);
- tf2.setEditable(false);
-
- btn1.setBounds(10, 135, 92, 25);
- btn1.addActionListener(this);
- btn2.setBounds(112, 135, 92, 25);
- btn2.addActionListener(this);
-
- fm.setBounds(100, 100, 220, 200);
- fm.setVisible(true);
- fm.setResizable(false);
- fm.setLayout(null);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(btn1);
- fm.add(btn2);
- fm.add(tf1);
- fm.add(tf2);
- fm.add(lb1);
- fm.add(lb2);
-
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==tf1 || e.getSource()==btn1){
- double area=Double.parseDouble(tf1.getText())*3.3058;
- String areaStr=String.valueOf(area);
- tf2.setText("面積為: "+areaStr+"平方公尺");
- }
- if(e.getSource()==btn2){
- tf1.setText("");
- tf2.setText("");
- }
- }
- public static void main(String[] args) {
- new Ch01();
- }
- }
複製代碼 |