返回列表 發帖
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTextField;
  7. public class Ch76 implements ActionListener
  8. {
  9.         JFrame fm;
  10.         JTextField tf2, tf1;
  11.         JLabel lb1, lb2;
  12.         JButton btn1, btn2;
  13.        
  14.             Ch76()
  15.             {
  16.                     tf1=new JTextField();
  17.                         tf1.setBounds(70, 45, 134, 30);
  18.                         tf1.addActionListener(this);
  19.                         tf2=new JTextField();
  20.                         tf2.setBounds(10, 85, 195, 40);
  21.             tf2.setEditable(false);
  22.                         tf2.addActionListener(this);
  23.                        
  24.                         lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  25.                         lb1.setBounds(0, 10, 215, 30);
  26.             lb2=new JLabel("輸入坪數:");
  27.                         lb2.setBounds(10, 40, 60, 40);
  28.                        
  29.                         btn1=new JButton("確定");
  30.                         btn1.setBounds(10, 135, 92, 25);
  31.                         btn1.addActionListener(this);
  32.                         btn2=new JButton("清除");
  33.                         btn2.setBounds(112, 135, 92, 25);
  34.                         btn2.addActionListener(this);

  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.                 {
  50.                         Ch76 app=new Ch76();
  51.         }
  52.                 public void actionPerformed(ActionEvent e)
  53.                 {
  54.                         if(e.getSource()==btn2)
  55.                         {
  56.                                 tf1.setText("");
  57.                                 tf2.setText("");
  58.                         }
  59.                         if(e.getSource()==btn1 || e.getSource()==tf1)
  60.                         {
  61.                                 Double area=Double.parseDouble(tf1.getText())*3.3058;
  62.                 tf2.setText("面積為: "+area+" 平方公尺");
  63.                         }
  64.                 }

  65. }
複製代碼

TOP

返回列表