返回列表 發帖

[隨堂練習] keyrelease

將上次做的坪數轉換的程式,透過keyrelease事件
將TextField中限制只允許數字
  1. package tw.kuas.edu.tw;

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;

  4. import javax.swing.JButton;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JTextField;
  8. import javax.swing.text.BadLocationException;

  9. import java.awt.event.KeyAdapter;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.Color;



  12. public class Main implements ActionListener{

  13.         public static void main(String[] args) {
  14.                 // TODO 自動產生的方法 Stub
  15.                 Main app = new Main();
  16.         }

  17.         @Override
  18.         public void actionPerformed(ActionEvent e) {
  19.                 // TODO 自動產生的方法 Stub
  20.                 if(e.getSource()==btn1)
  21.         {
  22.             tf2.setText("面積為: "+Double.parseDouble(tf1.getText())*3.3058+" 平方公尺");
  23.         }
  24.                 if(e.getSource()==btn2)
  25.                 {
  26.                         tf1.setText("");
  27.                         tf2.setText("");
  28.                 }
  29.         }
  30.         
  31.     private JFrame fm;
  32.     private JLabel lb1, lb2;
  33.     private JTextField tf1, tf2;
  34.     private JButton btn1, btn2;
  35.     private JButton btnNewButton;
  36.     private JButton btnNewButton_1;
  37.     Main()
  38.     {
  39.         lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  40.         lb1.setBounds(0, 10, 215, 30);
  41.         lb2=new JLabel("輸入坪數:");
  42.         lb2.setBounds(10, 40, 60, 40);
  43.         
  44.         tf1=new JTextField();
  45.         // keypress => 文字放上去後觸發
  46.         //
  47.         // keyrelease =>觸發後再將文字放上去
  48.         tf1.addKeyListener(new KeyAdapter() {
  49.                 @Override
  50.                 public void keyReleased(KeyEvent e) {
  51.                         int code = e.getKeyCode();
  52.                         if(code >= 97 && code <= 105)
  53.                         {
  54.                                 System.out.println(code);
  55.                         }
  56.                         else{
  57.                                 //先取得畫面上的文字內容
  58.                                 //刪除掉最後一個字
  59.                                 //刪除後的結果放到畫面上
  60.                                 int temp = tf1.getText().length();
  61.                                 try {
  62. //                                        char [] chr = tf1.getText().toCharArray();
  63. //                                        String ss ="";
  64. //                                        for(int i=0;i<chr.length-1;i++)
  65. //                                        {
  66. //                                                ss+=chr[i];
  67. //                                        }
  68.                                                 String str = tf1.getText(0, --temp);
  69.                                                 tf1.setText(str);
  70.                                         } catch (BadLocationException e1) {
  71.                                                 // TODO 自動產生的 catch 區塊
  72.                                                 e1.printStackTrace();
  73.                                         }
  74.                                
  75.                                
  76.                         }
  77.                 }
  78.         });
  79.         tf1.setBackground(Color.WHITE);
  80.         
  81.         tf1.setBounds(70, 45, 134, 30);
  82.         tf1.addActionListener(this);
  83.         
  84.         tf2=new JTextField();
  85.         tf2.setBounds(10, 85, 195, 40);
  86.         tf2.setEditable(false);
  87.         
  88.         
  89.         btn1=new JButton("確定");
  90.         btn1.setBounds(10, 135, 92, 25);
  91.         btn1.addActionListener(this);
  92.         btn2=new JButton("清除");
  93.         btn2.setBounds(112, 135, 92, 25);
  94.         btn2.addActionListener(this);
  95.         
  96.         fm=new JFrame("土地面積計算");
  97.         fm.setBounds(100, 100, 306, 306);
  98.         fm.setVisible(true);
  99.         fm.setResizable(false);
  100.         fm.getContentPane().setLayout(null);
  101.         fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  102.         fm.getContentPane().add(lb1);
  103.         fm.getContentPane().add(lb2);
  104.         fm.getContentPane().add(tf1);
  105.         fm.getContentPane().add(tf2);
  106.         fm.getContentPane().add(btn1);
  107.         fm.getContentPane().add(btn2);
  108.         
  109.       
  110.         
  111.       
  112.     }
  113. }
複製代碼

返回列表