返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Ch81 implements ActionListener
  4. {
  5.     JFrame f;
  6.     JButton clear,ok;
  7.     JLabel l1,l2;
  8.     JTextField in,out;
  9.     Ch81()
  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.         out=new JTextField();
  27.         out.setBounds(10,80,280,60);
  28.         out.setEditable(false);
  29.         /***********************************************/
  30.         ok=new JButton("確定");
  31.         ok.setBounds(10,150,100,30);
  32.         ok.addActionListener(this);

  33.         clear=new JButton("清除");
  34.         clear.setBounds(190,150,100,30);
  35.         clear.addActionListener(this);
  36.         /***********************************************/
  37.         f.add(l1);
  38.         f.add(l2);
  39.         f.add(in);
  40.         f.add(out);
  41.         f.add(ok);
  42.         f.add(clear);
  43.     }
  44.     public static void main(String args[])
  45.     {
  46.         new Ch81();
  47.     }
  48.     public void actionPerformed(ActionEvent e)
  49.     {
  50.         double n=0;
  51.         if(e.getSource()==ok)
  52.         {
  53.             n=Integer.parseInt(in.getText());
  54.             out.setText("面積為:"+(n*3.3058)+"平方公尺");
  55.         }
  56.         else
  57.         {
  58.             in.setText("");
  59.             out.setText("");
  60.         }
  61.     }
  62. }
複製代碼

TOP

返回列表