返回列表 發帖
  1. package lol;

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;

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

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

  65.         }

  66. }
複製代碼

TOP

返回列表