- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Ch81 implements ActionListener {
- JTextField t1, t2;
- JLabel lb1,lb2;
- JButton btn1,btn2;
- JFrame fm;
- void initialize(){
- t1=new JTextField();
- t2=new JTextField();
- t2.setEditable(false);
- t1.setBounds(70, 45, 134, 30);
- t2.setBounds(10, 85, 195, 40);
- lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
- lb1.setBounds(0, 10, 215, 30);
- lb2=new JLabel("輸入坪數:");
- lb2.setBounds(10, 40, 60, 40);
- btn1=new JButton("確定");
- btn1.setBounds(10, 135, 92, 25);
- btn2=new JButton("清除");
- btn2.setBounds(112, 135, 92, 25);
- fm=new JFrame("土地面積換算");
- fm.setBounds(100, 100, 220, 200);
- fm.setVisible(true);
- fm.setResizable(false);
- fm.setLayout(null);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(lb1);
- fm.add(lb2);
- fm.add(t1);
- fm.add(t2);
- fm.add(btn1);
- fm.add(btn2);
- }
- void act(){
- btn1.addActionListener(this);
- btn2.addActionListener(this);
- t1.addActionListener(this);
- }
- Ch81(){
- initialize();
- act();
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==t1 || e.getSource()==btn1)
- {
- double area=Double.parseDouble(t1.getText())*3.3058;
- String areaStr=String.valueOf(area);
- t2.setText("面積為: "+areaStr+" 平方公尺");
- }
- if(e.getSource()==btn2)
- {
- t1.setText("");
- t2.setText("");
- }
- }
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- Ch81 app = new Ch81();
- }
- }
複製代碼 |