返回列表 發帖
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;

  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTextField;


  7. public class Ch01 implements ActionListener{
  8.        
  9.         private JFrame fm=new JFrame("土筆面積換算");
  10.         private JLabel lb1=new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
  11.         private JLabel lb2=new JLabel("輸入坪數:");
  12.         private JTextField tf1=new JTextField();
  13.         private JTextField tf2=new JTextField();
  14.         private JButton btn1=new JButton("確定");
  15.         private JButton btn2=new JButton("清除");
  16.        
  17.         Ch01(){
  18.                
  19.                 lb1.setBounds(0, 10, 215, 30);
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1.setBounds(70, 45, 134, 30);
  23.                 tf1.addActionListener(this);
  24.                 tf2.setBounds(10, 85, 195, 40);
  25.                 tf2.setEditable(false);
  26.                
  27.                 btn1.setBounds(10, 135, 92, 25);
  28.                 btn1.addActionListener(this);
  29.                 btn2.setBounds(112, 135, 92, 25);
  30.                 btn2.addActionListener(this);
  31.                
  32.                 fm.setBounds(100, 100, 220, 200);
  33.                 fm.setVisible(true);
  34.                 fm.setResizable(false);
  35.                 fm.setLayout(null);
  36.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.                 fm.add(btn1);
  38.                 fm.add(btn2);
  39.                 fm.add(tf1);
  40.                 fm.add(tf2);
  41.                 fm.add(lb1);
  42.                 fm.add(lb2);
  43.                
  44.         }
  45.         @Override
  46.         public void actionPerformed(ActionEvent e) {
  47.                 if(e.getSource()==tf1 || e.getSource()==btn1){
  48.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  49.                         String areaStr=String.valueOf(area);
  50.                         tf2.setText("面積為: "+areaStr+"平方公尺");
  51.                 }
  52.                 if(e.getSource()==btn2){
  53.                         tf1.setText("");
  54.                         tf2.setText("");
  55.                 }
  56.         }
  57.         public static void main(String[] args) {
  58.                 new Ch01();
  59.         }
  60. }
複製代碼

TOP

返回列表