- package lol;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- public class Main implements ActionListener{
- JFrame fm;
- JLabel lb1;
- JLabel lb2;
- JButton bt1;
- JButton bt2;
- JTextField tf1;
- JTextField tf2;
- void area()
- {
- fm=new JFrame("土地面積計算");
- lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
- lb2=new JLabel("輸入坪數:");
- bt1=new JButton("確定");
- bt2=new JButton("清除");
- tf1=new JTextField();
- tf2=new JTextField();
- lb1.setBounds(0, 10, 215, 30);
- lb2.setBounds(10, 40, 60, 40);
- tf1.setBounds(70, 45, 134, 30);
- tf1.addActionListener(this);
- tf2.setBounds(10, 85, 195, 40);
- tf2.setEditable(false);
- bt1.setBounds(10, 135, 92, 25);
- bt1.addActionListener(this);
- bt2.setBounds(112, 135, 92, 25);
- bt2.addActionListener(this);
- fm.setBounds(100,100,220,200);
- fm.setResizable(false);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.setLayout(null);
- fm.setVisible(true);
- fm.add(lb1);
- fm.add(lb2);
- fm.add(bt1);
- fm.add(bt2);
- fm.add(tf1);
- fm.add(tf2);
- }
- Main()
- {
- area();
- }
- public static void main(String[] args) {
- new Main();
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==tf1||e.getSource()==bt1)
- {
- double a=Double.parseDouble(tf1.getText())*3.3058;
- tf2.setText("面積為: "+a+"平方公尺");
- }
- if(e.getSource()==bt2)
- {
- tf1.setText("");
- tf2.setText("");
- }
- }
- }
複製代碼 |