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

  64. }
複製代碼

TOP

返回列表