返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.ActionEvent;
  4. public class Ch80 implements ActionListener{
  5.         private JFrame fm1;
  6.         private JLabel lb1, lb2;
  7.         private JTextField tf3, tf2;
  8.         private JButton bt1, bt2;
  9.         Ch80(){
  10.                 fm1=new JFrame("土地面積運算");
  11.         lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  12.         lb2=new JLabel("輸入坪數:");
  13.         tf3=new JTextField();
  14.         tf2=new JTextField();
  15.         bt1=new JButton("確定");
  16.         bt2=new JButton("清除");
  17.         
  18.         bt1.setBounds(10,135,93,25);
  19.         bt1.addActionListener(this);
  20.         bt2.setBounds(113,135,93,25);
  21.         bt2.addActionListener(this);
  22.         
  23.         tf2.setBounds(70, 50, 135, 30);
  24.         tf2.addActionListener(this);
  25.         tf3.setBounds(10, 85, 194, 40);
  26.         tf3.setEditable(false);
  27.         
  28.         lb1.setBounds(10, 10, 195, 30);
  29.         lb2.setBounds(10, 50, 55, 30);
  30.         
  31.         fm1.setBounds(100, 100, 221, 200);
  32.         fm1.setVisible(true);
  33.         fm1.setResizable(false);
  34.         fm1.setLayout(null);
  35.         fm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.         fm1.add(lb1);
  37.         fm1.add(lb2);
  38.         fm1.add(bt1);
  39.         fm1.add(bt2);
  40.         fm1.add(tf2);
  41.         fm1.add(tf3);
  42.         }
  43.        
  44.         public void actionPerformed(ActionEvent e){
  45.                 if(e.getSource()==tf2 || e.getSource()==bt1){
  46.                         double area=Double.parseDouble(tf2.getText())*3.3058;
  47.                         tf3.setText("面積為: "+area+" 平方公尺");
  48.                 }
  49.                 if(e.getSource()==bt2){
  50.                         tf2.setText("");
  51.                         tf3.setText("");
  52.                 }
  53.         }
  54.         public static void main(String[] args) {
  55.                 Ch80 app=new Ch80();
  56.         }
  57. }
複製代碼
كخخخخخخخخخخخخخ

TOP

返回列表