- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Ch83 implements ActionListener{
-
- private JFrame fm1;
- private JButton btn1,btn2;
- private JTextField tf1,tf2;
- private JLabel lb1,lb2;
- Ch83(){
- JFrame fm1=new JFrame();
- JLabel lb1=new JLabel("一坪=3.3058平方公尺");
- JLabel lb2=new JLabel("輸入坪數:");
- JTextField tf1=new JTextField();
- JTextField tf2=new JTextField();
- JButton btn1=new JButton("確定");
- JButton btn2=new JButton("清除");
-
- tf1.setBounds(70,45,134,30);
- tf1.addActionListener(this);
- tf2.setBounds(10,85,195,40);
- tf2.setEditable(false);
-
- lb1.setBounds(45,10,215,30);
- lb2.setBounds(10,40,60,40);
-
- btn1.setBounds(10,135,92,25);
- btn1.addActionListener(this);
- btn2.setBounds(112,135,92,25);
- btn2.addActionListener(this);
-
- fm1.setTitle("JTextField test");
- fm1.setBounds(100,100,220,200);
- fm1.setVisible(true);
- fm1.setResizable(false);
- fm1.setLayout(null);
- fm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm1.add(lb1);
- fm1.add(lb2);
- fm1.add(tf1);
- fm1.add(tf2);
- fm1.add(btn1);
- fm1.add(btn2);
- }
- public void actionProfromed(ActionEvent e){
- if(e.getSource()==tf1 || e.getSource()== btn1){
- double asr=Double.parseDouble(tf1.getText())*3.3058;
- String layasr=String.valueOf(asr);
- tf2.setText("面積為:"+layasr+"平方公分");
- }
- if(e.getSource()==btn2){
- tf1.setText("");
- tf2.setText("");
- }
- }
- public static void main(String[] args){
- Ch83 app=new Ch83();
- }
- }
複製代碼 |