捕获所有输入到JTextField

时间:2011-12-21 13:53:25

标签: java swing jtextfield

您好我在我的swing形式中使用了4个JTextField。我需要做的是通过按Enter键和tab键将焦点从一个JTextField移动到另一个JTextField.i成功完成但问题是JTextField只允许数字值如果我在J Textfield中给出除数值以外的任何值,则JTextField被锁定,并且JTextField之间没有焦点转移。我使用的代码。

package focus;
    import java.awt.Component;
    import java.awt.FlowLayout;
    import java.awt.event.FocusAdapter;
    import java.awt.event.FocusEvent;
    import java.awt.event.KeyAdapter.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.text.JTextComponent;

    public class Main implements KeyListener{
        JTextField txt1 = new JTextField(20);
        JTextField txt2 = new JTextField(20);
        JTextField txt3 = new JTextField(20);
        JTextField txt4 = new JTextField(20);
        JFrame f = new JFrame();
        public Main()
        {

         txt1.addFocusListener(new MyFocusListener<JTextField>());
        txt2.addFocusListener(new MyFocusListener<JTextField>());
        txt3.addFocusListener(new MyFocusListener<JTextField>());
        txt4.addFocusListener(new MyFocusListener<JTextField>());
         txt1.addKeyListener(this);
          txt2.addKeyListener(this);
           txt3.addKeyListener(this);
         txt4.addKeyListener(this);
        f.setLayout(new FlowLayout());
        f.add(txt1);
        f.add(txt2);
         f.add(txt3);
          f.add(txt4);
        f.pack();
        f.setVisible(true);}

      public static void main(String[] argv) throws Exception {
          new Main();

      }

        public void keyTyped(KeyEvent e) {

        }

        public void keyPressed(KeyEvent ke) {
                    if (ke.getSource() == txt1)
      {
        if (ke.getKeyCode() == KeyEvent.VK_ENTER)
        {
          txt2.requestFocusInWindow();
          txt2.selectAll();
        }
      }
      else if (ke.getSource() == txt2)

      {
        if (ke.getKeyCode() == KeyEvent.VK_ENTER)
        {
          txt3.requestFocusInWindow();
          txt3.selectAll();
        }
      }
      else if (ke.getSource() == txt3)
      {
        if (ke.getKeyCode() == KeyEvent.VK_ENTER)
        {
          txt4.requestFocusInWindow();
          txt4.selectAll();
              }
      }
    else if (ke.getSource() == txt4)
      {
        if (ke.getKeyCode() == KeyEvent.VK_ENTER)
        {
          txt1.requestFocusInWindow();
          txt1.selectAll();
              }
      }

        }

        public void keyReleased(KeyEvent e) {

        }
    }
    class MyFocusListener <C extends JTextField> extends  FocusAdapter {
      //boolean showingDialog = false;
      public void focusGained(FocusEvent evt) {
        final JTextComponent c = (JTextComponent) evt.getSource();
        String s = c.getText();
        c.requestFocus();
        c.selectAll();
        for (int i = 0; i < s.length(); i++) {
       if (!Character.isDigit(s.charAt(i))) {
       c.setSelectionStart(i);
        c.setSelectionEnd(i);
           break;
       }
        }
      }

      public void focusLost(FocusEvent evt) {
        final JTextComponent c = (JTextComponent) evt.getSource();
        String s = c.getText();

       if (evt.isTemporary()) {
         return;
        }
        for (int i = 0; i < s.length(); i++) {
       if (!Character.isDigit(s.charAt(i))) {
           //System.out.println("must only contain digits");
            c.requestFocus();
            c.selectAll();
            break;
          }
      }
        }

    }

2 个答案:

答案 0 :(得分:2)

就像是因为你让它表现得像那样:)

FIX:focusLost中,替换if (!Character.isDigit(s.charAt(i)))

if (!Character.isLetterOrDigit(s.charAt(i)))


 public void focusLost(FocusEvent evt) {
        final JTextComponent c = (JTextComponent) evt.getSource();
        String s = c.getText();

       if (evt.isTemporary()) {
         return;
        }
        for (int i = 0; i < s.length(); i++) {
       if (!Character.isLetterOrDigit(s.charAt(i))) {

            c.requestFocus();
            c.selectAll();
            break;
          }
      }
        }

或简单地说,

  

删除整个内容:)查看评论。

答案 1 :(得分:1)

  1. 无需致电c.requestFoucs()中的focusGained()。组件已经成为焦点,这就是调用此方法的原因。
  2. 这是一个假设,但可能是正确的:“你正在做的是检查textfield中是否包含所有数字字符,如果没有,则在c.requestFocus()方法中调用focusLost()。所以一旦焦点是从组件(textField)丢失,因为包含非数字字符,它再次获得焦点,你看不到任何焦点遍历。“