JAVA、Netbeans:如何在 jtextfield 中分配标签索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12948043/
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-10-31 10:54:46  来源:igfitidea点击:

JAVA, Netbeans: how do I assign a tab index in a jtextfield?

javanetbeansjtextfield

提问by Weddy

I searched for it and didn't find the right answer. Some say that the arrangement of the components in the inspector tab makes an automatic tab index. But it didn't work. So is there really a tab property in netbeans?

我搜索它并没有找到正确的答案。有人说检查器选项卡中组件的排列使自动选项卡索引。但它没有用。那么,netbeans 中真的有 tab 属性吗?

回答by Agung Setiawan

just use this piece of code and it works, using setNextFocusableComponent()

只需使用这段代码,它就可以工作,使用setNextFocusableComponent()

buttonA.setNextFocusableComponent(buttonB);
buttonB.setNextFocusableComponent(buttonC);

回答by Mike Player NZ

I was going to comment on Yogendra Singh's answer, but I've only just signed up & can't. Yogendra's answer is almost perfect, but doesn't take into account what happens if someone changes focus by clicking on another component. The focusNumber will still be set to the old component, and pressing tab will take you to the wrong component.

我打算对 Yogendra Singh 的回答发表评论,但我刚刚注册并且不能。Yogendra 的答案几乎是完美的,但没有考虑如果有人通过单击另一个组件来改变焦点会发生什么。focusNumber 仍将设置为旧组件,按 Tab 键会将您带到错误的组件。

Modifying the answer slightly fixes this issue:

修改答案稍微解决了这个问题:

List<Component> elementList = new ArrayList<Component>();
elementList.Add(element1);
elementList.Add(element2);
...
elementList.Add(elementx);
setFocusTraversalPolicy(new CustomFocusTraversalPolicy());


private class CustomFocusTraversalPolicy extends FocusTraversalPolicy {   

    public Component getComponentAfter(Container focusCycleRoot,Component aComponent)    {   
        int currentPosition = elementList.indexOf(aComponent);
        currentPosition = (currentPosition + 1) % elementList.size();   
        return (Component)elementList.get(currentPosition);   
    }   
    public Component getComponentBefore(Container focusCycleRoot,Component aComponent)    { 
        int currentPosition = elementList.indexOf(aComponent);
        currentPosition = (elementList.size() + currentPosition - 1) % elementList.size();   
        return (Component)elementList.get(currentPosition);   
    }
    public Component getFirstComponent(Container cntnr) {
        return (Component)elementList.get(0);
    }
    public Component getLastComponent(Container cntnr) {
        return (Component)elementList.get(focusList.size() - 1);
    }
    public Component getDefaultComponent(Container cntnr) {
        return (Component)elementList.get(0);
    }
}

回答by Yogendra Singh

Define Component[] elementsListto hold your elements, extend FocusTraversalPolicyand set the elements in desired order using elementsListe.g.

定义Component[] elementsList以保存您的元素,FocusTraversalPolicy使用elementsList例如扩展和设置所需的顺序元素

 Component[] elementsList = new Component[]{elem1,elem2,elem3};
 setFocusTraversalPolicy(new MyFocusTraversalPolicy());


 public class CustomFocusTraversalPolicy extends  FocusTraversalPolicy    {   

  public Component getComponentAfter(Container focusCycleRoot,Component aComponent)    {   
   focusNumber = (focusNumber+1) % focusList.length;   
   return (JTextField)focusList[focusNumber];   
  }   
  public Component getComponentBefore(Container focusCycleRoot,Component aComponent)    { 
   focusNumber = (focusList.length+focusNumber-1) % focusList.length;   
   return (JTextField)focusList[focusNumber];   
 }
}

回答by Joop Eggen

The GUI editor generates code in initComponents with:

GUI 编辑器在 initComponents 中生成代码:

jTabbedPane1.addTab("tab1", jTextField1);

This does not seem to be customizable, using an insertTabwith index. (It would have been shown in the Properties in the Layout group.)

这似乎不可定制,使用insertTabwith 索引。(它会显示在布局组的属性中。)

So the best is to generate the tab component entirely in code, with, insertTabor:

所以最好是完全在代码中生成选项卡组件,insertTab或者:

jTabbedPane1.add(jTextField1, index);

回答by Simon Pio.

The answer of Mike Plazer NZ has a small mistake I guess.

我猜 Mike Plazer NZ 的回答有一个小错误。

public Component getLastComponent(Container cntnr) {
    return (Component)elementList.get(focusList.size() - 1);
}

should be

应该

public Component getLastComponent(Container cntnr) {
    return (Component)elementList.get(elementList.size() - 1);
}

the "focusList.size()" was changed to "elementList.size()"

“focusList.size()”已更改为“elementList.size()”

Otherwise eclipse shows an error. But a great answer!

否则 eclipse 会显示错误。但是一个很好的答案!