返回列表 發帖
  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import javax.swing.JTextField;
  4. import javax.swing.JLabel;

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

TOP

返回列表