gpt4 book ai didi

java - 自定义 FocusTraversalPolicy 类 Java Swing

转载 作者:太空宇宙 更新时间:2023-11-04 07:55:56 25 4
gpt4 key购买 nike

我正在努力为应用程序中的多个面板设置新的选项卡顺序。我想创建一个新类,它允许我以与已弃用的 setNextFocusableComponent 方法相同的方式处理排序。我正在创建一个扩展 DefaultTreversalPolicyClass 并重载 getComponentBefore 和 getComponentAfter 方法的类。在面板本身上,我希望实现像创建一个新的自定义类(我将其称为“CustomizedTabOrderPolicy”)一样简单,然后添加我想要为其指定 Tab 键顺序的组件。我通过 CustomizedTabOrderPolicy 类上的一个便捷方法来完成此操作,该方法允许我将要为其指定 Tab 键顺序“链接”的组件添加到哈希表,然后创建一个哈希表的副本,在其中切换键和值。然后,我使用这些哈希表在两个组件之间建立向前和向后 Tab 键顺序“链接”。

以下是我的自定义类:

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 方法时,它们永远不会被触发。我对使用焦点遍历策略非常陌生,所以我想知道我在这里缺少什么......

最佳答案

好吧,所以我应该做的是继承 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;
}
}

关于java - 自定义 FocusTraversalPolicy 类 Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13634502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com