0

我在屏幕上有很多文本字段(比代码显示的要多)。

private Component[] focusList;
focusList = new Component[]{
        txtArcustNo,
        txtBillTo,
        txtAcctNo,
        txtName,
        txtAddress,
        txtAddress2,
        txtAddress3,
        txtAddress4,
        txtContact,
        txtContact2,
        txtEmail,
        txtWebsite,
        txtPhone,
        txtPhone1Ext,
        txtPhone2,
        txtPhone2Ext,
        txtFax1,
        txtFax1Ext,
        txtFax2,
        txtFax2Ext,
    };
    focusTraversal = new WWFocusTraversalPolicy(focusList);
    pnlBase.setFocusTraversalPolicy(focusTraversal);

在一个条件下(即用户在屏幕上标记一个复选框)我想从焦点中删除 txtPhone2 和 txtPhone2Ext。但是,如果我在 onClick 事件中明确说将这两个字段设置为 Focusable false,那么当我在字段中切换时,我的光标只会卡在 txtPhone1Ext 并且不会转到 Fax1 字段,从而跳过我设置的字段不可聚焦。

想知道我是否缺少某些东西。任何提示/建议表示赞赏。如果重要,我正在使用 NetBeans IDE。

4

1 回答 1

4

阅读关于如何使用焦点子系统的 Swing 教程。它提供了如何创建自定义焦点遍历策略的示例。不幸的是,该示例不是很完整,因为它也存在与您的代码相同的问题,因为它始终假定组件能够获得焦点。

我修改了示例中的类以确保组件可聚焦:

public static class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy
{
    Vector<Component> order;

    public MyOwnFocusTraversalPolicy(Vector<Component> order)
    {
        this.order = new Vector<Component>(order.size());
        this.order.addAll(order);
    }

    public Component getComponentAfter(Container focusCycleRoot,
                                       Component aComponent)
    {
//      int idx = (order.indexOf(aComponent) + 1) % order.size();
//      return order.get(idx);

        int idx = order.indexOf(aComponent);

        for (int i = 0; i < order.size(); i++)
        {
            idx = (idx + 1) % order.size();
            Component next = order.get(idx);

            if (canBeFocusOwner(next)) return next;
        }

        return null;
    }

    public Component getComponentBefore(Container focusCycleRoot,
                                        Component aComponent)
    {
/*
        int idx = order.indexOf(aComponent) - 1;
        if (idx < 0) {
            idx = order.size() - 1;
        }
        return order.get(idx);
*/
        int idx = order.indexOf(aComponent);

        for (int i = 0; i < order.size(); i++)
        {
            idx = (idx - 1);

            if (idx < 0)
            {
                idx = order.size() - 1;
            }

            Component previous = order.get(idx);

            if (canBeFocusOwner(previous)) return previous;
        }

        return null;
    }

    public Component getDefaultComponent(Container focusCycleRoot) {
//      return order.get(0);
        return getFirstComponent( focusCycleRoot );
    }

    public Component getLastComponent(Container focusCycleRoot) {
//      return order.lastElement();

        Component c = order.lastElement();

        if (canBeFocusOwner(c))
            return c;
        else
            return getComponentBefore(focusCycleRoot, c);
    }

    public Component getFirstComponent(Container focusCycleRoot)
    {
//      return order.get(0);

        Component c = order.get(0);

        if (canBeFocusOwner(c))
            return c;
        else
            return getComponentAfter(focusCycleRoot, c);
    }

    private boolean canBeFocusOwner(Component c)
    {
        if (c.isEnabled() && c.isDisplayable() && c.isVisible() && c.isFocusable())
        {
            return true;
        }

        return false;
    }

}
于 2013-09-17T18:10:01.220 回答