返回列表 發帖
  1. package bbs.istak.org.tw;

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;

  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JTextField;
  11. import javax.swing.text.BadLocationException;



  12. public class actionTest implements ActionListener{

  13.         protected static final char[] code = null;
  14.                 public static void main(String[] args) {
  15.                 // TODO 自動產生的方法 Stub
  16.                 actionTest app = new actionTest();
  17.         }

  18.         @Override
  19.         public void actionPerformed(ActionEvent e) {
  20.                 // TODO 自動產生的方法 Stub
  21.                 if(e.getSource()==btn1)
  22.         {
  23.             tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
  24.         }
  25.                 if(e.getSource()==btn2)
  26.                 {
  27.                         tf1.setText("");
  28.                         tf2.setText("");
  29.                 }
  30.         }
  31.         
  32.         private JFrame fm;
  33.     private JLabel lb1, lb2;
  34.     private JTextField tf1, tf2;
  35.     private JButton btn1, btn2;
  36.     actionTest()
  37.     {
  38.             lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  39.         lb1.setBounds(0, 10, 215, 30);
  40.         lb2=new JLabel("輸入坪數:");
  41.         lb2.setBounds(10, 40, 60, 40);
  42.         
  43.         tf1=new JTextField();
  44.                 tf1.addKeyListener(new KeyAdapter(){
  45.                         public void KeyReleased (KeyEvent e){
  46.                                
  47.                                 if(e.getKeyCode()>=96 || e.getKeyCode()<=105)
  48.                                 {
  49.                                         System.out.println(code);
  50.                                 }
  51.                                 else{
  52.                                         int str = tf1.getText().length();
  53.                                         try {
  54.                                                 tf2.setText(tf1.getText(0, --str));
  55.                                                
  56.                                         }catch(BadLocationException e1){
  57.                                                 e1.printStackTrace();
  58.                                                
  59.                                         }
  60.                                        
  61.                                                

  62.                                                
  63.                                         }
  64.                                 }
  65.                                
  66.        
  67.                
  68.                
  69.                
  70.         });
  71.         
  72.         
  73.         tf1.setBounds(70, 45, 134, 30);
  74.         tf1.addActionListener(this);
  75.         tf2=new JTextField();
  76.         tf2.setBounds(10, 85, 195, 40);
  77.         tf2.setEditable(false);
  78.         
  79.         btn1=new JButton("確定");
  80.         btn1.setBounds(10, 135, 92, 25);
  81.         btn1.addActionListener(this);
  82.         btn2=new JButton("清除");
  83.         btn2.setBounds(112, 135, 92, 25);
  84.         btn2.addActionListener(this);
  85.         
  86.         fm=new JFrame("土地面積計算");
  87.         fm.setBounds(100, 100, 220, 200);
  88.         fm.setVisible(true);
  89.         fm.setResizable(false);
  90.         fm.setLayout(null);
  91.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  92.         fm.add(lb1);
  93.         fm.add(lb2);
  94.         fm.add(tf1);
  95.         fm.add(tf2);
  96.         fm.add(btn1);
  97.         fm.add(btn2);
  98.     }

  99. }
複製代碼

TOP

返回列表