这个阻塞GlassPane 有问题吗? [英] Is there a problem with this blocking GlassPane?

查看:22
本文介绍了这个阻塞GlassPane 有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I found this blocking GlassPane class on the web and I'm curious to know if some of you see any problem with it.

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();
            }
        }
    }
}

推荐答案

一目了然,我在这里看到了几个问题,主要是在 eventDispatched() 方法中和周围.

Just at a glance, I see several problems here, mostly in and around the eventDispatched() method.

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

First, why are you implementing AWTEventListener at all, since you never add this object to anything as an AWTEventListener? Did you mean to add this object to itself as an event listener? Are you adding it as an event listener somewhere else in code that isn't shown here?

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

Second, why do you test e instanceof EventObject? I cut-and-pasted your code into Eclipse, which immediately warned me that all AWTEvent objects are instances of EventObject. So, you can get rid of that test - It will always be true.

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

Third, why on earth are you resorting to reflection? It looks as if you are trying to use a Swing-only method on AWT events that don't have it. That approach won't work - Trying to reflectively call a nonexistent method will just throw an exception, which this code will silently catch and ignore.

最后,你为什么要重新发明轮子?一些快速的谷歌搜索揭示了一些更简单的例子和一些更复杂的示例,您可以将其用作您工作的起点和这可能会让你更接近你真正想要的东西.

Finally, why are you reinventing the wheel? Some quick Googling reveals some simpler examples and some more complicated examples that you could use as a starting point for your work and which would likely get you much closer to what you really want here.

这篇关于这个阻塞GlassPane 有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆