1

我正在为一个应用程序中的多个面板设置新的选项卡顺序。我想创建一个新类,它允许我以与不推荐使用的 setNextFocusableComponent 方法相同的方式处理排序。我正在创建一个扩展 DefaultTreversalPolicyClass 并重载 getComponentBefore 和 getComponentAfter 方法的类。在面板本身上,我希望实现与创建一个新的自定义类(我将其称为 CustomizedTabOrderPolicy)一样简单,然后添加我要为其指定 Tab 键顺序的组件。我通过 CustomizedTabOrderPolicy 类上的一种便捷方法来执行此操作,该方法允许我将要为其指定 Tab 键顺序“链接”的组件添加到 Hashtable,然后创建键和值在其中切换的 hastable 的副本。

以下是我的自定义类:

import java.awt.Component;
import java.awt.Container;
import java.awt.DefaultFocusTraversalPolicy;
import java.util.Hashtable;

public class CustomizedTabOrderPolicy extends DefaultFocusTraversalPolicy {

    private Hashtable<Component, Component> exceptionsMap = new Hashtable<Component, Component>();
    private Hashtable<Component, Component> excpetionsMapReflection = new Hashtable<Component, Component>();
    private Component defaultComponent;
    private Component firstComponent;
    private Component lastComponent;

    public CustomizedTabOrderPolicy() {
    }

    @Override
    public Component getComponentAfter(Container container, Component component) {
        java.awt.Toolkit.getDefaultToolkit().beep();
        if (exceptionsMap.containsKey(component)) {
            return exceptionsMap.get(component);
        } else {
            return super.getComponentAfter(container, component);
        }
    }

    @Override
    public Component getComponentBefore(Container container, Component component) {
        if (exceptionsMap.containsValue(component)) {
            return excpetionsMapReflection.get(component);
        } else {
            return super.getComponentBefore(container, component);
        }
    }

    @Override
    public Component getDefaultComponent(Container container) {
        if (this.defaultComponent != null) {
            return this.defaultComponent;
        }
        return super.getDefaultComponent(container);
    }

    @Override
    public Component getFirstComponent(Container container) {
        if (this.firstComponent != null) {
            return this.firstComponent;
        }
        return super.getFirstComponent(container);
    }

    @Override
    public Component getLastComponent(Container container) {
        if (this.lastComponent != null) {
            return this.lastComponent;
        }
        return super.getLastComponent(container);
    }

    public void addNewException(Component origin, Component destination) {
        exceptionsMap.put(origin, destination);
        excpetionsMapReflection.put(destination, origin);
    }
    public void setDefaultComponent(Component defaultComponent) {
        this.defaultComponent = defaultComponent;
    }

    public void setLastComponent(Component lastComponent) {
        this.lastComponent = lastComponent;
    }

    public void setFirstComponent(Component firstComponent) {
        this.firstComponent = firstComponent;
    }
}

为了实现这一点,我在我的 JPanel 上使用以下内容:

CustomizedTabOrderPolicy customPolicy = new CustomizedTabOrderPolicy();

customPolicy.addNewException(<component1>, <component2>);
customPolicy.addNewException(<component3>, <component4>);
customPolicy.addNewException(<component5>, <component6>);

this.setFocusTraversalPolicy(customPolicy);

我没有收到任何编译器或运行时错误,但 JPanel 似乎根本没有使用我的 CustomTabOrderPolicy。当我在 getComponentBefore 和 getComponentAfter 方法中插入断点时,它们永远不会被触发。我对使用焦点遍历策略非常陌生,所以我想知道我在这里缺少什么......

4

2 回答 2

1

好的,所以我应该做的是继承 FocusTraversalPolicy 而不是 DefaultFocusTraversalPolicy 并传入原始策略的副本以在没有“异常”时恢复。我还需要在面板上将 FocusCycleRoot 设置为 true,以便它使用我的 FocusTraversalPolicy。我现在已经设置好了,这样我就有了我的 AbstractJPanel 基类上的 CustomizedTabOrderPolicy 实例,所以我需要在面板初始化中调用是这样的:

    this.setTabOrderEnabled(true);
    this.getCustomTabOrder().addNewException(component1, component2);
    this.getCustomTabOrder().addNewException(component2, component3);
    this.getCustomTabOrder().addNewException(component3, component4);

我还对课程本身进行了一些更改以使其更有效。

public class CustomizedTabOrderPolicy extends FocusTraversalPolicy {

    private FocusTraversalPolicy oldPolicy;
    private Hashtable<Component, Component> exceptionsMap = new Hashtable<Component, Component>();
    private Hashtable<Component, Component> exceptionsMapReflection = new Hashtable<Component, Component>();
    private Component defaultComponent;
    private Component firstComponent;
    private Component lastComponent;

    public CustomizedTabOrderPolicy(FocusTraversalPolicy oldPolicy) {
        this.oldPolicy = oldPolicy;
    }

    public Component getComponentAfter(Container container, Component component) {
        if (component == this.getLastComponent(container)) {
            if (isValid(this.getFirstComponent(container))) {
                return this.getFirstComponent(container);
            } else {
                return this.getComponentAfter(container, this.getFirstComponent(container));
            }
        }
        if (exceptionsMap.containsKey(component) && isValid(exceptionsMap.get(component))) {
            return exceptionsMap.get(component);
        } else {
            return oldPolicy.getComponentAfter(container, component);
        }
    }

    public Component getComponentBefore(Container container, Component component) {
        if (component == this.getFirstComponent(container)) {
            if (isValid(this.getLastComponent(container))) {
                return this.getLastComponent(container);
            } else {
                return this.getComponentBefore(container, this.getLastComponent(container));
            }
        }
        if (exceptionsMapReflection.containsKey(component) && isValid(exceptionsMapReflection.get(component))) {
            return exceptionsMapReflection.get(component);
        } else {
            return oldPolicy.getComponentBefore(container, component);
        }
    }

    public boolean isValid(Component component) {
        if (component.isEnabled() && component.isVisible() && component.isFocusable()) {
            return true;
        } else {
            return false;
        }
    }
    public Component getDefaultComponent(Container container) {
        if (this.defaultComponent != null) {
            return this.defaultComponent;
        }
        return oldPolicy.getDefaultComponent(container);
    }

    public Component getFirstComponent(Container container) {
        if (this.firstComponent != null) {
            return this.firstComponent;
        }
        return oldPolicy.getFirstComponent(container);
    }

    public Component getLastComponent(Container container) {
        if (this.lastComponent != null) {
            return this.lastComponent;
        } else {
            return oldPolicy.getLastComponent(container);
        }
    }

    public void addNewException(Component origin, Component destination) {
        exceptionsMap.put(origin, destination);
        exceptionsMapReflection.put(destination, origin);
    }
    public void setDefaultComponent(Component defaultComponent) {
        this.defaultComponent = defaultComponent;
    }

    public void setLastComponent(Component lastComponent) {
        this.lastComponent = lastComponent;
    }

    public void setFirstComponent(Component firstComponent) {
        this.firstComponent = firstComponent;
    }
 }
于 2012-11-30T20:38:48.890 回答
0

高效的!我使用它稍作修改,让它在不是 isValid() 时继续遍历(当然它应该在您的面板中至少有一个 isValid() 组件):

public Component getComponentAfter(Container container, Component component) {
    if (component == this.getLastComponent(container)) {
        if (isValid(this.getFirstComponent(container))) return this.getFirstComponent(container);
        else                                            return this.getComponentAfter(container, this.getFirstComponent(container));
    }
    if( exceptionsMap.containsKey(component) ) {
        component = exceptionsMap.get(component);
        if( isValid(component) ) return component;
        return this.getComponentAfter(container, component);
    }
    return oldPolicy.getComponentAfter(container, component);
}
public Component getComponentBefore(Container container, Component component) {
    if (component == this.getFirstComponent(container)) {
        if (isValid(this.getLastComponent(container))) return this.getLastComponent(container);
        else                                           return this.getComponentBefore(container, this.getLastComponent(container));
    }
    if( exceptionsMapReflection.containsKey(component) ) {
        component = exceptionsMapReflection.get(component);
        if( isValid(component) ) return component;
        return this.getComponentBefore(container, component);
    }
    return oldPolicy.getComponentBefore(container, component);
}
于 2014-07-11T11:14:21.137 回答