返回列表 發帖
本帖最後由 鄞美旭 於 2023-4-22 12:14 編輯
  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 A implements ActionListener{
  8.         JFrame fm;
  9.         JLabel lb1,lb2;
  10.         JTextField tf1,tf2;
  11.         JButton btn1,btn2;
  12.         A()
  13.         {
  14.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  15.                 lb1.setBounds(0, 10, 215, 30);
  16.                 lb2=new JLabel("輸入坪數:");
  17.                 lb2.setBounds(10, 40, 65, 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.setEditable(false);
  25.                
  26.                 btn1=new JButton("確定");
  27.                 btn1.setBounds(10, 135, 92, 25);
  28.                 btn1.addActionListener(this);
  29.                 btn2=new JButton("清除");
  30.                 btn2.setBounds(112, 135, 92, 25);;
  31.                 btn2.addActionListener(this);
  32.                
  33.                 fm=new JFrame("土地面積換算");
  34.                 fm.setBounds(100, 100, 220, 200);
  35.                 fm.setVisible(true);
  36.                 fm.setResizable(false);
  37.                 fm.setLayout(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 static void main(String[] args) {
  47.                 A app=new A();

  48.         }

  49.                 @Override
  50.                 public void actionPerformed(ActionEvent e) {
  51.                         if(e.getSource()==tf1 || e.getSource()==btn1)
  52.                         {
  53.                                 double area=Double.parseDouble(tf1.getText())*3.3058;
  54.                                 String areastr=String.valueOf(area);
  55.                                 tf2.setText("面積為: "+areastr+"平方公尺");
  56.                         }
  57.                         if(e.getSource()==btn2)
  58.                         {
  59.                                 tf1.setText("");
  60.                                 tf2.setText("");
  61.                         }
  62.                 }

  63. }
複製代碼

TOP

本帖最後由 鄞美旭 於 2023-4-22 12:13 編輯
  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 A{

  8.         JFrame fm;
  9.         JLabel lb1, lb2;
  10.         JTextField tf1, tf2;
  11.         JButton btn1, btn2;

  12.         A()
  13.         {
  14.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  15.                 lb1.setBounds(0, 10, 215, 30);
  16.                 lb2=new JLabel("輸入坪數:");
  17.                 lb2.setBounds(10, 40, 65, 40);

  18.                 tf1=new JTextField();
  19.                 tf1.setBounds(70, 45, 134, 30);
  20.                 tf1.addActionListener(new ActionListener() {
  21.                         @Override
  22.                         public void actionPerformed(ActionEvent e) {
  23.                                 double area=Double.parseDouble(tf1.getText())*3.3058;
  24.                                 tf2.setText(String.format("面積為: %.2f平方公尺", area));      
  25.                         }
  26.                 });
  27.                 tf2=new JTextField();
  28.                 tf2.setBounds(10, 85, 195, 40);
  29.                 tf2.setEditable(false);

  30.                 btn1=new JButton("確定");
  31.                 btn1.setBounds(10, 135, 92, 25);
  32.                 btn1.addActionListener(new ActionListener() {
  33.                         @Override
  34.                         public void actionPerformed(ActionEvent e) {
  35.                                 double area=Double.parseDouble(tf1.getText())*3.3058;
  36.                                 tf2.setText(String.format("面積為: %.2f平方公尺", area));
  37.                         }
  38.                 });
  39.                 btn2=new JButton("清除");
  40.                 btn2.setBounds(112, 135, 92, 25);
  41.                 btn2.addActionListener(new ActionListener() {
  42.                         @Override
  43.                         public void actionPerformed(ActionEvent e) {
  44.                                 tf1.setText("");
  45.                                 tf2.setText("");
  46.                         }
  47.                 });

  48.                 fm=new JFrame("土地面積換算");
  49.                 fm.setBounds(100, 100, 220, 200);
  50.                 fm.setVisible(true);
  51.                 fm.setResizable(false);
  52.                 fm.setLayout(null);
  53.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.                 fm.add(lb1);
  55.                 fm.add(lb2);
  56.                 fm.add(tf1);
  57.                 fm.add(tf2);
  58.                 fm.add(btn1);
  59.                 fm.add(btn2);
  60.         }

  61.         public static void main(String[] args) {
  62.                 new A();   
  63.         }
  64. }
複製代碼

TOP

返回列表