- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.*;;
- import java.awt.event.ActionEvent;
- public class Ch39 implements ActionListener{
- private JFrame fm;
- private JLabel lb1,lb2;
- private JTextField tf1,tf2;
- private JButton btn1,btn2;
- Ch39()
- {
- lb1=new JLable("1坪=3.3058cm*cm",JLabel.CENTER);
- lb1.setBounds(0, 10, 215, 30);
- lb2=new JLabel("輸入坪數");
- lb2.setBounds(10, 40, 60, 40);
-
- tf1=new JTextField();
- tf1.setBounds(70, 45, 134, 30);
- tf1.addActionListener(this);
- tf2=new JTextField();
- tf2.setBounds(10, 85, 195, 40);
- tf2.addActionListener(false);
-
- btn1=new JButton("yes");
- btn1.setBounds(10, 135, 92, 25);
- btn1.addActionListener(this);
- btn2=new JButton("no");
- btn2.setBounds(112, 135, 92, 25);
- btn2.addActionListener(this);
-
- fm=new JFrame("ground cm*cm count");
- fm.setBounds(100, 100, 220, 200);
- fm.setVisible(true);
- fm.Resizeable(false);
- fm.Layout(null);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(lb1);
- fm.add(lb2);
- fm.add(tf1);
- fm.add(tf2);
- fm.add(btn1);
- fm.add(btn2);
- }
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==tf1 || e.getSource()==btn1)
- {
- double area=Double.parseDouble(tf1.getText())*3.3058;
- String sresStr=String.valueOf(area);
- tf2.setText("cm*cm="+areaStr+"cm*cm");
- }
- if(e.getSource()==btn2)
- {
- tf1.setText("");
- tf2.setText("");
- }
- }
- public static void main(String[] args)
- {
- Ch39 app=new Ch39();
- }
- }
複製代碼 |