返回列表 發帖
  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.         JFrame fm;
  10.         JLabel lb1,lb2;
  11.         JTextField tf1,tf2;
  12.         JButton btn1,btn2;
  13.        
  14.         Ch01()
  15.         {
  16.                 lb1=new JLabel("1坪=3.358平方公尺",JLabel.CENTER);
  17.                 lb1.setBounds(0, 10, 215, 30);
  18.                 lb1.setVisible(true);
  19.                
  20.                 lb2=new JLabel("輸入坪數:");
  21.                 lb2.setBounds(10, 40, 60, 40);
  22.                 lb2.setVisible(true);
  23.                
  24.                 tf1=new JTextField();
  25.                 tf1.setBounds(70, 45, 134, 30);
  26.                 tf1.setVisible(true);
  27.                 tf1.addActionListener(this);
  28.                
  29.                 tf2=new JTextField();
  30.                 tf2.setBounds(10, 85, 195, 40);
  31.                 tf2.setEditable(true);
  32.                 tf2.setVisible(true);
  33.                 tf2.addActionListener(this);
  34.                
  35.                 btn1=new JButton("確定");
  36.                 btn1.setBounds(10, 135, 92, 25);
  37.                 btn1.setVisible(true);
  38.                 btn1.addActionListener(this);
  39.                
  40.                 btn2=new JButton("清除");
  41.                 btn2.setBounds(112, 135, 92, 25);
  42.                 btn2.setVisible(true);
  43.                 btn2.addActionListener(this);
  44.                
  45.                 fm=new JFrame("土地面積計算");
  46.                 fm.setBounds(100,100,233,233);
  47.                 fm.setVisible(true);
  48.                 fm.setResizable(false);
  49.                 fm.setLayout(null);
  50.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.                 fm.add(btn1);
  52.                 fm.add(btn2);
  53.                 fm.add(lb1);
  54.                 fm.add(lb2);
  55.                 fm.add(tf1);
  56.                 fm.add(tf2);
  57.                
  58.         }
  59.         public static void main(String args[])
  60.         {
  61.                 new Ch01();
  62.         }
  63.         public void actionPerformed(ActionEvent e) {
  64.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  65.                 {
  66.                         double result=Double.parseDouble(tf1.getText())*3.3058;
  67.                     tf2.setText("面積為:"+result+"平方公尺");
  68.                 }
  69.                 if(e.getSource()==btn2)
  70.                 {
  71.                         tf1.setText("");
  72.                         tf2.setText("");
  73.                 }
  74.         }
  75. }
複製代碼

TOP

返回列表