6

我有一个 Swing 应用程序的问题,由于我目前无法识别的原因,有时会通过文本输入字段获得焦点。我怀疑某种竞争条件,但我看不出是什么导致了焦点事件。

该字段附加了一个焦点侦听器,因此可以直接向 focusGained() 事件处理程序添加断点。当我这样做时,我可以看到底层事件又包含 CausedFocusEvent.Cause 的一个实例。名称字段设置为“激活”。

查看堆栈跟踪,我可以看到以下内容:

Thread [AWT-EventQueue-0] (Suspended (breakpoint at line 174 in MyPanel$3)) 
    MyPanel$3.focusGained(FocusEvent) line: 174 
    AWTEventMulticaster.focusGained(FocusEvent) line: not available 
    InputField(Component).processFocusEvent(FocusEvent) line: not available 
    InputField(Component).processEvent(AWTEvent) line: not available    
    InputField(Container).processEvent(AWTEvent) line: not available    
    InputField(Component).dispatchEventImpl(AWTEvent) line: not available   
    InputField(Container).dispatchEventImpl(AWTEvent) line: not available   
    InputField(Component).dispatchEvent(AWTEvent) line: not available   
    DefaultKeyboardFocusManager(KeyboardFocusManager).redispatchEvent(Component, AWTEvent) line: not available  
    DefaultKeyboardFocusManager.typeAheadAssertions(Component, AWTEvent) line: not available    
    DefaultKeyboardFocusManager.dispatchEvent(AWTEvent) line: not available 
    InputField(Component).dispatchEventImpl(AWTEvent) line: not available   
    InputField(Container).dispatchEventImpl(AWTEvent) line: not available   
    InputField(Component).dispatchEvent(AWTEvent) line: not available   
    SunToolkit$1.run() line: not available  
    PeerEvent(InvocationEvent).dispatch() line: not available   
    EventQueue.dispatchEventImpl(AWTEvent, Object) line: not available  
    EventQueue.access$200(EventQueue, AWTEvent, Object) line: not available 
    EventQueue$3.run() line: not available  
    EventQueue$3.run() line: not available  
    AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]    
    ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: not available 
    EventQueue.dispatchEvent(AWTEvent) line: not available  
    EventDispatchThread.pumpOneEventForFilters(int) line: not available 
    EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available  
    EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available 
    EventDispatchThread.pumpEvents(int, Conditional) line: not available    
    EventDispatchThread.pumpEvents(Conditional) line: not available 
    EventDispatchThread.run() line: not available   

请注意,InputField 是 JTextField 的子类,它与文本的呈现相关的细微变化。

我可以从堆栈跟踪中得知 EDT 上的某些内容导致 MyPanel 中的 InputField 获得焦点。

是否有任何进一步的信息可以让我了解为什么该组件获得了焦点?

4

1 回答 1

0

我有类似的问题,

最后,我只是指定了焦点层次结构,并从 java 中控制了所有焦点。它有点像这样:

    // Setting The FOCUS Order.
    Vector<Component> order = new Vector<Component>(7);
    order.add(projectNameJTextField);
    order.add(companyJTextField);
    order.add(orderedByJTextField);
    order.add(phoneJTextField);
    order.add(jRadioNormalPriority);
    order.add(jRadioHighPriority);
    order.add(jCheckBox1);
    order.add(jCheckBox2);
    order.add(jCheckBox3);
    order.add(jCheckBox4);
    order.add(jCheckBox5);
    order.add(jCheckBox6);
    order.add(textDetailJTextArea);
    order.add(technitianJComboBox);
    order.add(aproxTimeJTextField);
    order.add(sendButton);

    // Sending the costume focus order to the focus Policy Class,
    MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy(order);
    setFocusTraversalPolicy(newPolicy);

你还需要这个类:

    import java.awt.Component;
    import java.awt.Container;
    import java.awt.FocusTraversalPolicy;
    import java.util.Vector;



    // This Class Helps to Set A Costume FOCUS Order.
    public 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);
    }

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

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

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

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

(这不是我的解决方案,我不记得我从哪里得到的)

希望我能帮忙:)

戴夫。

于 2014-09-16T12:48:25.127 回答