- import javax.swing.*;
- import java.awt.event.*;
- public class Ch82 implements ActionListener
- {
- JFrame f;
- JButton clear,ok;
- JLabel l1,l2;
- JTextField in,out;
- Ch82()
- {
- f=new JFrame("A\u05a4\u0201");
- f.setVisible(true);
- f.setBounds(100,100,300,300);
- f.setLayout(null);
- f.setResizable(false);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- /***********************************************/
- l1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
- l1.setBounds(0,0,294,30);
- /***********************************************/
- l2=new JLabel("輸入坪數:");
- l2.setBounds(10,40,90,30);
- /***********************************************/
- in=new JTextField();
- in.setBounds(80,40,200,30);
- in.addActionListener(this);
-
- out=new JTextField();
- out.setBounds(10,80,280,60);
- out.setEditable(false);
- /***********************************************/
- ok=new JButton("確定");
- ok.setBounds(10,150,100,30);
- ok.addActionListener(this);
- clear=new JButton("清除");
- clear.setBounds(190,150,100,30);
- clear.addActionListener(this);
- /***********************************************/
- f.add(l1);
- f.add(l2);
- f.add(in);
- f.add(out);
- f.add(ok);
- f.add(clear);
- }
- public static void main(String args[])
- {
- new Ch82();
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==ok||e.getSource()==in)
- {
- double n=Double.parseDouble(in.getText())*3.3058;
- out.setText("面積為:"+n+"平方公尺");
- }
- else
- {
- in.setText("");
- out.setText("");
- }
- }
- }
複製代碼 |