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

  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.JTextField;
  6. import javax.swing.JButton;
  7. public class Ch13 implements ActionListener{
  8.         
  9.         JFrame fm;
  10.         JLabel lb1, lb2;
  11.         JTextField tf1, tf2;
  12.         JButton btn1, btn2;
  13.         
  14.         Ch13()
  15.         {
  16.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  17.                 lb1.setBounds(0, 10, 215, 30);
  18.                 lb2=new JLabel("輸入坪數:");
  19.                 lb2.setBounds(10, 40, 60, 40);
  20.                
  21.                 tf1=new JTextField();
  22.                 tf1.setBounds(70, 45, 134, 30);
  23.                 tf1.addActionListener(this);
  24.                 tf2=new JTextField();
  25.                 tf2.setBounds(10, 85, 195, 40);
  26.                 tf2.setEditable(false);
  27.                
  28.                 btn1=new JButton("確定");
  29.                 btn1.setBounds(10, 135, 92, 25);
  30.                 btn1.addActionListener(this);
  31.                 btn2=new JButton("清除");
  32.                 btn2.setBounds(112, 135, 92, 25);
  33.                 btn2.addActionListener(this);
  34.                
  35.                 fm=new JFrame("土地面積計算");
  36.                 fm.setBounds(100, 100, 220, 200);
  37.                 fm.setVisible(true);
  38.                 fm.setResizable(false);
  39.                 fm.setLayout(null);
  40.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41.                 fm.add(lb1);
  42.                 fm.add(lb2);
  43.                 fm.add(tf1);
  44.                 fm.add(tf2);
  45.                 fm.add(btn1);
  46.                 fm.add(btn2);
  47.         }

  48.         public static void main(String[] args) {
  49.                 Ch13 app=new Ch13();
  50.         }

  51.                 @Override
  52.                 public void actionPerformed(ActionEvent e) {
  53.                         if(e.getSource()==tf1 || e.getSource()==btn1)
  54.             {
  55.                     double pig=Double.parseDouble(tf1.getText())*3.3058;
  56.                     tf2.setText("面積為: "+pig+" 平方公尺");
  57.             }      
  58.             if(e.getSource()==btn2)
  59.             {
  60.                     tf1.setText("");
  61.                     tf2.setText("");
  62.             }
  63.                        
  64.                 }

  65. }
複製代碼
hahahahahahahaha

TOP

返回列表