将所有输入捕获到 JTextField 中

发布于 2024-12-22 10:56:10 字数 3578 浏览 1 评论 0原文

您好,我在我的 swing 表单中使用了 4 个 JTextField。我需要做的是通过按 Enter 键和 Tab 键将焦点从一个 JTextField 移动到另一个 JTextField。我成功完成了它,但问题是 JTextField 只允许数字值,如果我在 J Textfield 中给出除数值以外的任何值,JTextField 被锁定,并且 JTextFields 之间不会发生焦点转移。我使用的代码。

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;
          }
      }
        }

    }

Hi i used 4 JTextField in my swing form.i need to do is moving the focus from one JTextField to the other JTextField by hit the enter key and tab key.i done it successfully but the problem was the JTextField allows only numeric values if i give any value other than numeric value in the J Textfield the JTextField is locked and there is no focus transfer happen between the JTextFields.the code i was used .

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;
          }
      }
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初熏 2024-12-29 10:56:10

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

修复: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;
          }
      }
        }

或简单地,

删除整个内容:)请参阅评论。

It was like that because you made it behave like that :)

FIX : In focusLost, replace the if (!Character.isDigit(s.charAt(i)))

with

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;
          }
      }
        }

or simply,

remove the whole thing :) see the comments.

骷髅 2024-12-29 10:56:10
  1. 无需在 focusGained() 中调用 c.requestFoucs()。组件已经获得焦点,这就是调用此方法的原因。
  2. 这是一个假设,但可能是正确的:“您正在做的是检查文本字段中是否包含所有数字字符,如果没有,则在 focusLost()< 中调用 c.requestFocus() /code> 方法。因此,一旦组件(textField)失去焦点,并且由于包含非数字字符,它会再次获得焦点,并且您看不到任何焦点遍历。”
  1. No need to call c.requestFoucs() in focusGained(). Component already got the focus, that's why this method is called.
  2. This is an assumption, but might be correct: "What you are doing is checking if textfield has all numeric characters in it and if not then call c.requestFocus() in focusLost() method. So once focus is lost from component(textField) and because contains non-numeric characters it again gains focus and you can't see any focus traversal."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文