3

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

4

5 回答 5

4

只需使用这段代码就可以了,使用setNextFocusableComponent ()

buttonA.setNextFocusableComponent(buttonB);
buttonB.setNextFocusableComponent(buttonC);
于 2013-01-02T11:39:46.550 回答
2

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

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

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);
    }
}
于 2014-05-17T09:52:39.467 回答
1

定义以保存您的元素,使用例如以所需的顺序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];   
 }
}
于 2012-10-18T06:17:43.870 回答
1

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

jTabbedPane1.addTab("tab1", jTextField1);

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

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

jTabbedPane1.add(jTextField1, index);
于 2012-10-18T13:29:47.133 回答
0

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

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

应该

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

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

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

于 2016-02-13T18:23:02.083 回答