我搜索过它并找不到合适的答案。有人说,检查器选项卡中组件的排列会生成自动选项卡索引。但它没有用。 netbeans中真的有一个tab属性吗?
答案 0 :(得分:4)
只需使用这段代码即可使用 setNextFocusableComponent ()
buttonA.setNextFocusableComponent(buttonB);
buttonB.setNextFocusableComponent(buttonC);
答案 1 :(得分: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);
}
}
答案 2 :(得分: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];
}
}
答案 3 :(得分:1)
GUI编辑器使用以下命令在initComponents中生成代码:
jTabbedPane1.addTab("tab1", jTextField1);
使用带有索引的insertTab
似乎无法自定义。 (它将显示在布局组中的属性中。)
所以最好的方法是使用insertTab
或:
jTabbedPane1.add(jTextField1, index);
答案 4 :(得分: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会显示错误。 但是一个很好的答案!