我正在创建 java 桌面应用程序。我想更改面板组件的标签顺序。我创建了自己的焦点遍历策略。在面板中,我使用两个日期选择器字段。其他组件完全获得焦点,但 datechooser 字段没有获得焦点。我试过了
List<Component> order=new LinkedList<>();
order.add(pidcmb);
order.add(locationtxt);
order.add(chemicalnametxt);
order.add(fromtxt);//datechooser
order.add(totxt);//datechooser
order.add(generate);
order.add(cancel);
newPolicy = new MyOwnFocusTraversalPolicy(order);
parameter.setFocusTraversalPolicy(newPolicy);
parameter.setFocusCycleRoot(true);
并创建一个类
class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy
{
List<Component> order;
public MyOwnFocusTraversalPolicy(List<Component> order) {
this.order = new LinkedList<Component>() {};
this.order.addAll(order);
}
@Override
public Component getComponentAfter(Container focusCycleRoot,
Component aComponent)
{
int idx = (order.indexOf(aComponent) + 1) % order.size();
return order.get(idx);
}
@Override
public Component getComponentBefore(Container focusCycleRoot,
Component aComponent)
{
int idx = order.indexOf(aComponent) - 1;
if (idx < 0) {
idx = order.size() - 1;
}
return order.get(idx);
}
@Override
public Component getDefaultComponent(Container focusCycleRoot) {
return order.get(0);
}
@Override
public Component getLastComponent(Container focusCycleRoot) {
return order.get(order.size());
}
@Override
public Component getFirstComponent(Container focusCycleRoot) {
return order.get(0);
}
}