返回列表 發帖
  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 Ch01 implements ActionListener
  8. {
  9.       
  10.         private JFrame fm;
  11.         private JLabel lb1, lb2;
  12.         private JTextField tf1, tf2;
  13.         private JButton btn1, btn2;
  14.       
  15.         Ch01()
  16.         {
  17.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  18.                 lb1.setBounds(0, 10, 215, 30);
  19.                 lb2=new JLabel("輸入坪數:");
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1=new JTextField();
  23.                 tf1.setBounds(70, 45, 134, 30);
  24.                 tf1.addActionListener(this);
  25.                 tf2=new JTextField();
  26.                 tf2.setBounds(10, 85, 195, 40);
  27.                 tf2.setEditable(false);
  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.                
  36.                 fm=new JFrame("土地面積計算");
  37.                 fm.setBounds(100, 100, 220, 200);
  38.                 fm.setVisible(true);
  39.                 fm.setResizable(false);
  40.                 fm.setLayout(null);
  41.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.                 fm.add(lb1);
  43.                 fm.add(lb2);
  44.                 fm.add(tf1);
  45.                 fm.add(tf2);
  46.                 fm.add(btn1);
  47.                 fm.add(btn2);
  48.         }
  49.       
  50.         public void actionPerformed(ActionEvent e)
  51.         {
  52.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  53.                 {
  54.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  55.                         String areaStr=String.valueOf(area);
  56.                         //String areaStr=Double.toString(area);
  57.                         tf2.setText("面積為: "+areaStr+" 平方公尺");
  58.                 }      
  59.                 if(e.getSource()==btn2)
  60.                 {
  61.                         tf1.setText("");
  62.                         tf2.setText("");
  63.                 }
  64.         }

  65.         public static void main(String[] args) {
  66.                 Ch01 app=new Ch01();
  67.         }

  68. }
複製代碼

TOP

  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 Ch01 implements ActionListener
  8. {
  9.       
  10.         private JFrame fm;
  11.         private JLabel lb1, lb2;
  12.         private JTextField tf1, tf2;
  13.         private JButton btn1, btn2;
  14.       
  15.         Ch01()
  16.         {
  17.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  18.                 lb1.setBounds(0, 10, 215, 30);
  19.                 lb2=new JLabel("輸入坪數:");
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1=new JTextField();
  23.                 tf1.setBounds(70, 45, 134, 30);
  24.                 tf1.addActionListener(new ActionListener() {
  25.                         @Override
  26.                         public void actionPerformed(ActionEvent e) {
  27.                                 double area=Double.parseDouble(tf1.getText())*3.3058;
  28.                                 tf2.setText(String.format("面積為: %.2f平方公尺", area));      
  29.                         }
  30.                 });
  31.                 tf2=new JTextField();
  32.                 tf2.setBounds(10, 85, 195, 40);
  33.                 tf2.setEditable(false);
  34.                
  35.                 btn1=new JButton("確定");
  36.                 btn1.setBounds(10, 135, 92, 25);
  37.                 btn1.addActionListener(new ActionListener() {
  38.                         @Override
  39.                         public void actionPerformed(ActionEvent e) {
  40.                                 double area=Double.parseDouble(tf1.getText())*3.3058;
  41.                                 tf2.setText(String.format("面積為: %.2f平方公尺", area));
  42.                         }
  43.                 });
  44.                btn2 = new JButton("清除");
  45.                btn2.setBounds(112,135,92,25);
  46.                btn2.addActionListener(new ActionListener() {
  47.                    @Override
  48.                    public void actionPerformed(ActionEvent e) {
  49.                            tf1.setText("");
  50.                            tf2.setText("");
  51.                    }
  52.            });

  53.                 fm=new JFrame("土地面積計算");
  54.                 fm.setBounds(100, 100, 220, 200);
  55.                 fm.setVisible(true);
  56.                 fm.setResizable(false);
  57.                 fm.setLayout(null);
  58.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.                 fm.add(lb1);
  60.                 fm.add(lb2);
  61.                 fm.add(tf1);
  62.                 fm.add(tf2);
  63.                 fm.add(btn1);
  64.                 fm.add(btn2);
  65.         }
  66.       
  67.         public void actionPerformed(ActionEvent e)
  68.         {
  69.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  70.                 {
  71.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  72.                         String areaStr=String.valueOf(area);
  73.                                                                                                           //String areaStr=Double.toString(area);
  74.                         tf2.setText("面積為: "+areaStr+" 平方公尺");
  75.                 }      
  76.                 if(e.getSource()==btn2)
  77.                 {
  78.                         tf1.setText("");
  79.                         tf2.setText("");
  80.                 }
  81.         }

  82.         public static void main(String[] args) {
  83.                 Ch01 app=new Ch01();
  84.         }

  85. }
複製代碼

TOP

返回列表