Java:光标如何自动从一个 TextField 移动到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9819238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:05:30  来源:igfitidea点击:

Java: How cursor automatically move from one TextField to other

javaswingawt

提问by Vinit Vikash

In my application four TextArea is there and I want to enter only four character in one Text area and cursor automatically move to next TestArea. Again when I enter four character in this TextArea then again cursor automatically move to next TextArea.

在我的应用程序中,有四个 TextArea,我只想在一个 Text 区域中输入四个字符,并且光标会自动移动到下一个 TestArea。再次当我在此 TextArea 中输入四个字符时,光标再次自动移动到下一个 TextArea。

Example: At the time of installing Window XP it want "Key" and there are four section when you enter four character in first section then cursor automatically move to the next section.

示例:安装Window XP 时需要“Key”,第一部分输入四个字符时有四个部分,然后光标自动移动到下一个部分。

Same thing I want in my application.

我在我的应用程序中想要的东西。

For this first of all I add CustomizedTextFields.jar and then created four IntegerField:

为此,我首先添加了 CustomizedTextFields.jar,然后创建了四个 IntegerField:

private IntegerField text1;
private IntegerField text2;
private IntegerField text3;
private IntegerField text4;

after this I show all these IntegerField on my frame.

在此之后,我将所有这些 IntegerField 显示在我的框架上。

Now I tried this code to send cursor to the next field but it's not working:

现在我尝试使用此代码将光标发送到下一个字段,但它不起作用:

text1.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                    int a2 = text1.getText().length();
                    if (a2 == 3) {
                        text2.getCursor();
                    }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }
        });

采纳答案by kleopatra

interesting enough question to try improving my shadowy knowledge of the text package :-)

足够有趣的问题来尝试提高我对文本包的模糊知识:-)

There are two separate requirements here

这里有两个单独的要求

  • restrict the lenght of the text: that's done with a DocumentFilter as @mKorbel already noted
  • automatically transferFocus to the next component after the max length is reached: turns out that can be done with a NavigationFilter
  • 限制文本的长度:正如@mKorbel 已经指出的那样,这是使用 DocumentFilter 完成的
  • 达到最大长度后自动将焦点转移到下一个组件:事实证明可以使用 NavigationFilter 来完成

in code:

在代码中:

JComponent panel = new JPanel();
final int maxSize = 3;
for (int i = 0; i < 4; i++) {
    final JTextField field = new JTextField(5);
    NavigationFilter filter = new NavigationFilter() {

        @Override
        public void setDot(FilterBypass fb, int dot, Bias bias) {
            if (dot >= maxSize) {
                fb.setDot(0, bias);
                field.transferFocus();
                return;
            }
            fb.setDot(dot, bias);
        }

        @Override
        public void moveDot(FilterBypass fb, int dot, Bias bias) {
            if (dot >= maxSize) { 
                fb.setDot(0, bias);
                field.transferFocus();
                return;
            }
            fb.moveDot(dot, bias);
        }

    };
    field.setNavigationFilter(filter);
    ((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentSizeFilter(maxSize));
    panel.add(field);
}

The documentFilter is the one from the Swing Tutorial

documentFilter 是Swing 教程中的一个

回答by socha23

Replace text2.getCursor()with text2.requestFocus().

替换text2.getCursor()text2.requestFocus()

getCursor()is for retrieving the shape of the mouse pointer when hovering over a component.

getCursor()用于在悬停在组件上时检索鼠标指针的形状。

Also, with this method it is still possible to enter more than 4 chars in a field, for example by pasting from clipboard. If you want to block that, you would need to check if text entered is longer than 4 chars, and if so, take only first 4 chars from it.

此外,使用这种方法仍然可以在一个字段中输入 4 个以上的字符,例如通过从剪贴板粘贴。如果你想阻止它,你需要检查输入的文本是否超过 4 个字符,如果是,只取前 4 个字符。

回答by John Snow

Something like this should work:

这样的事情应该工作:

text1.addKeyListener(new KeyAdapter(){
          public void keyPressed(KeyEvent e){
             String value=text1.getText();
             if(value.length()==4){
             text2.requestFocus();
          }
}

Where text2 is your next textfield

其中 text2 是您的下一个文本字段

回答by mKorbel

At the time of installing Window XP it want "Key" and there are four section 
when you enter four character in first section then cursor automatically move 
to the next section.
  1. add DocumentListenerto the JTextComponents, for listening add DocumentFilter

  2. don't use KeyListenerfor JTextComponents, use only DocumentListener

  3. add required next JTextAreato the DocumentListener, if is there typed 4th. Char into JTextArea,

  4. notice, moving with Focusfrom one JTextAreato another would be better wrapped into invokeLater

  1. DocumentListener添加到JTextComponents,用于监听添加DocumentFilter

  2. 不要KeyListener用于JTextComponents,只使用DocumentListener

  3. 旁边添加需要JTextAreaDocumentListener,如果是有类型的第四位。字符变成JTextArea,

  4. 注意,Focus从一个移动JTextArea到另一个会更好地包裹在invokeLater

回答by Vishal Menaria

simply just create textarea and go to key typed events den u may write this

只需创建 textarea 并转到关键类型事件 den 你可以写这个

String number=jTextArea1.getText();
 int l=number.length();
 if(l==3){
 jTextArea1.transferFocus();

 }