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

  5. import java.awt.*;;
  6. import java.awt.event.ActionEvent;
  7. public class Ch39 implements ActionListener{

  8.         private JFrame fm;
  9.         private JLabel lb1,lb2;
  10.         private JTextField tf1,tf2;
  11.         private JButton btn1,btn2;
  12.         Ch39()
  13.         {
  14.                 lb1=new JLable("1坪=3.3058cm*cm",JLabel.CENTER);
  15.                 lb1.setBounds(0, 10, 215, 30);
  16.                 lb2=new JLabel("輸入坪數");
  17.                 lb2.setBounds(10, 40, 60, 40);
  18.                
  19.                 tf1=new JTextField();
  20.                 tf1.setBounds(70, 45, 134, 30);
  21.                 tf1.addActionListener(this);
  22.                 tf2=new JTextField();
  23.                 tf2.setBounds(10, 85, 195, 40);
  24.                 tf2.addActionListener(false);
  25.                
  26.                 btn1=new JButton("yes");
  27.                 btn1.setBounds(10, 135, 92, 25);
  28.                 btn1.addActionListener(this);
  29.                 btn2=new JButton("no");
  30.                 btn2.setBounds(112, 135, 92, 25);
  31.                 btn2.addActionListener(this);
  32.        
  33.             fm=new JFrame("ground cm*cm count");
  34.             fm.setBounds(100, 100, 220, 200);
  35.             fm.setVisible(true);
  36.             fm.Resizeable(false);
  37.             fm.Layout(null);
  38.             fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.             fm.add(lb1);
  40.             fm.add(lb2);
  41.             fm.add(tf1);
  42.             fm.add(tf2);
  43.             fm.add(btn1);
  44.             fm.add(btn2);
  45.         }
  46.         public  void actionPerformed(ActionEvent e) {
  47.        if(e.getSource()==tf1 || e.getSource()==btn1)
  48.        {
  49.                double area=Double.parseDouble(tf1.getText())*3.3058;
  50.                String sresStr=String.valueOf(area);
  51.                tf2.setText("cm*cm="+areaStr+"cm*cm");
  52.        }
  53.        if(e.getSource()==btn2)
  54.        {
  55.                tf1.setText("");
  56.                tf2.setText("");
  57.        }
  58.         }
  59.     public static void main(String[] args)
  60.     {
  61.             Ch39 app=new Ch39();
  62.     }
  63. }
複製代碼

TOP

返回列表