【问题标题】:Is there a problem with this blocking GlassPane?这个阻塞 GlassPane 有问题吗?
【发布时间】:2010-03-09 22:33:07
【问题描述】:

我在网络上发现了这个阻塞 GlassPane 类,我很想知道你们中的一些人是否发现它有任何问题。

public final class BlockingGlassPane extends JComponent implements AWTEventListener {

// Events will be consumed for this window.
private Window parentWindow;
// Focus will be returned to this component.
private Component lastFocusOwner;

private final Toolkit toolkit;

public BlockingGlassPane() {
    super();
    setOpaque(false);
    addMouseListener(new MouseAdapter() {
    });
    addKeyListener(new KeyAdapter() {
    });
    setInputVerifier(new InputVerifier() {
        @Override
        public boolean verify(JComponent anInput) {
            return false;
        }
    });
    toolkit = Toolkit.getDefaultToolkit();
}

@Override
public void setVisible(boolean b) {
    if (b) {
        if (parentWindow == null) {
            parentWindow = SwingUtilities.windowForComponent(this);
        }
        Component focusOwner = parentWindow.getFocusOwner();
        if (focusOwner != this) {
            lastFocusOwner = focusOwner;
        }
        toolkit.addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
        toolkit.addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK);
        requestFocus();
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    } else {
        toolkit.removeAWTEventListener(this);
        if (lastFocusOwner != null) {
            lastFocusOwner.requestFocus();
            lastFocusOwner = null;
        }
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
    super.setVisible(b);
}

@SuppressWarnings("unchecked")
public void eventDispatched(AWTEvent e) {
    Object source = e.getSource();
    if (e instanceof EventObject && source instanceof Component) {
        Component src = (Component) source;
        EventObject ev = e;
        if (SwingUtilities.windowForComponent(src) == parentWindow) {
            try {
                Class[] cls = {};
                Object[] args = {};
                ev.getClass().getMethod("consume", cls).invoke(ev, args);
            } catch (Exception ex) {
                // ex.printStackTrace();
            }
        }
    }
}

【问题讨论】:

    标签: java swing


    【解决方案1】:

    乍一看,我发现这里有几个问题,主要是在 eventDispatched() 方法中和周围。

    首先,你为什么要实现AWTEventListener,因为你从来没有把这个对象作为AWTEventListener添加到任何东西上?您的意思是将此对象作为事件侦听器添加到自身吗?您是否将其作为事件侦听器添加到此处未显示的代码中的其他位置?

    第二,为什么要测试e instanceof EventObject?我将您的代码剪切并粘贴到 Eclipse 中,它立即警告我所有 AWTEvent 对象都是 EventObject 的实例。所以,你可以摆脱那个测试——它永远都是真的。

    第三,你到底为什么要诉诸反思?看起来好像您正在尝试对没有它的 AWT 事件使用仅限 Swing 的方法。这种方法行不通 - 尝试反射性地调用不存在的方法只会引发异常,此代码将静默捕获并忽略该异常。

    最后,你为什么要重新发明轮子?一些快速的谷歌搜索揭示了一些 simpler examples 和一些 more complicated examples,您可以将它们用作您工作的起点,这可能会让您更接近您真正想要的。

    【讨论】:

    • 我不喜欢这种语气,我很清楚我不是这段代码的作者,但无论如何感谢链接。
    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 2015-08-17
    • 1970-01-01
    • 2017-05-17
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    相关资源
    最近更新 更多