返回列表 發帖
  1. package ch6666;

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

  67. }
複製代碼

TOP

返回列表