返回列表 發帖
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;

  3. import javax.swing.*;
  4. import javax.swing.text.BadLocationException;

  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. public class CALC implements ActionListener{

  8.         public static void main(String[] args)
  9.         {
  10.                 // TODO 自動產生的方法 Stub
  11.         CALC calc = new CALC();
  12.         }

  13.         @Override
  14.         public void actionPerformed(ActionEvent e)
  15.         {
  16.                 // TODO 自動產生的方法 Stub
  17.                 if(e.getSource()==bt1)
  18.                 {
  19.                         tf2.setText("面積為 "+Double.parseDouble(tf1.getText())*3.3058+" sqmeter.");
  20.                 }       
  21.                 if(e.getSource()==bt2)
  22.                 {
  23.                         tf1.setText("");
  24.                         tf2.setText("");
  25.                 }       
  26.                
  27.         }
  28.        
  29.         private JFrame fm;
  30.         private JLabel lb1;
  31.         private JLabel lb2;
  32.         private JTextField tf1;
  33.         private JTextField tf2;
  34.         private JButton bt1;
  35.     private JButton bt2;
  36.    
  37.     CALC()
  38.     {
  39.             fm=new JFrame("土地面積計算");
  40.         lb1=new JLabel("1坪=3.3058sqmeter");
  41.         lb2=new JLabel("輸入坪數:");
  42.         tf1=new JTextField();
  43.         tf1.addKeyListener(new KeyAdapter() {
  44.                 @Override
  45.                 public void keyReleased(KeyEvent arg0) {
  46.                         int kc=arg0.getKeyCode();  
  47.                                            
  48.                         if(kc==8 || 96<= kc && kc<=105 || kc==110)
  49.                         {
  50.                                 System.out.println("y");
  51.                         }       
  52.                         else
  53.                         {
  54.                                 int length = tf1.getText().length();
  55.                                 String str;
  56.                                         try
  57.                                         {
  58.                                                 System.out.println("n");
  59.                                                 str = tf1.getText(0, --length);
  60.                                                 tf1.setText(str);
  61.                                         }
  62.                                         catch (BadLocationException e)
  63.                                         {
  64.                                                 // TODO 自動產生的 catch 區塊
  65.                                                 e.printStackTrace();
  66.                                         }
  67.                                
  68.                                  
  69.                         }
  70.                 }
  71.         });
  72.      
  73.         tf2=new JTextField();
  74.         bt1=new JButton("確定");
  75.         bt2=new JButton("清除");
  76.         
  77.         lb1.setBounds(45, 10, 215, 30);
  78.         lb2.setBounds(10, 40, 60, 40);
  79.         
  80.         tf1.setBounds(70, 45, 125, 30);
  81.         tf2.setBounds(10, 85, 185, 40);
  82.         tf1.setEditable(true);
  83.         tf2.setEditable(false);
  84.         tf1.addActionListener(this);
  85.         tf2.addActionListener(this);
  86.         
  87.         bt1.setBounds(10, 135, 92, 25);
  88.         bt2.setBounds(105, 135, 92, 25);
  89.         bt1.addActionListener(this);
  90.         bt2.addActionListener(this);
  91.         
  92.         fm.setBounds(100, 100, 220, 200);
  93.         fm.setVisible(true);
  94.         fm.setResizable(true);
  95.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  96.         fm.getContentPane().setLayout(null);
  97.         fm.getContentPane().add(tf1);
  98.         fm.getContentPane().add(tf2);
  99.         fm.getContentPane().add(lb1);
  100.         fm.getContentPane().add(lb2);         
  101.         fm.getContentPane().add(bt1);
  102.         fm.getContentPane().add(bt2);
  103.         }

  104. }
複製代碼

TOP

返回列表