返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Ch82 implements ActionListener
  4. {
  5.     JFrame f;
  6.     JButton clear,ok;
  7.     JLabel l1,l2;
  8.     JTextField in,out;
  9.     Ch82()
  10.     {
  11.         f=new JFrame("A\u05a4\u0201");
  12.         f.setVisible(true);
  13.         f.setBounds(100,100,300,300);
  14.         f.setLayout(null);
  15.         f.setResizable(false);
  16.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.         /***********************************************/
  18.         l1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  19.         l1.setBounds(0,0,294,30);
  20.         /***********************************************/
  21.         l2=new JLabel("輸入坪數:");
  22.         l2.setBounds(10,40,90,30);
  23.         /***********************************************/
  24.         in=new JTextField();
  25.         in.setBounds(80,40,200,30);
  26.         in.addActionListener(this);
  27.         
  28.         out=new JTextField();
  29.         out.setBounds(10,80,280,60);
  30.         out.setEditable(false);
  31.         /***********************************************/
  32.         ok=new JButton("確定");
  33.         ok.setBounds(10,150,100,30);
  34.         ok.addActionListener(this);

  35.         clear=new JButton("清除");
  36.         clear.setBounds(190,150,100,30);
  37.         clear.addActionListener(this);
  38.         /***********************************************/
  39.         f.add(l1);
  40.         f.add(l2);
  41.         f.add(in);
  42.         f.add(out);
  43.         f.add(ok);
  44.         f.add(clear);
  45.     }
  46.     public static void main(String args[])
  47.     {
  48.         new Ch82();
  49.     }
  50.     public void actionPerformed(ActionEvent e)
  51.     {

  52.         if(e.getSource()==ok||e.getSource()==in)
  53.         {
  54.             double n=Double.parseDouble(in.getText())*3.3058;
  55.             out.setText("面積為:"+n+"平方公尺");
  56.         }
  57.         else
  58.         {
  59.             in.setText("");
  60.             out.setText("");
  61.         }
  62.     }
  63. }
複製代碼

TOP

返回列表