1

简而言之 - 我想要的是在被模态 JDialog 阻止的窗口上捕获鼠标单击。

这是一个例子:

public class BlockedFrameTest
{
    public static void main ( final String[] args )
    {
        Toolkit.getDefaultToolkit ().addAWTEventListener ( new AWTEventListener ()
        {
            @Override
            public void eventDispatched ( final AWTEvent event )
            {
                if ( event instanceof MouseEvent )
                {
                    System.out.println ( event );
                }
            }
        }, AWTEvent.MOUSE_EVENT_MASK );

        final JFrame frame = new JFrame ( "Frame" );
        frame.add ( new JLabel ( "Content" )
        {
            {
                setBorder ( BorderFactory.createEmptyBorder ( 100, 100, 100, 100 ) );
            }
        } );
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        frame.setVisible ( true );

        final JDialog dialog = new JDialog ( frame, "Dialog" );
        dialog.setModal ( true );
        dialog.add ( new JLabel ( "Content" )
        {
            {
                setBorder ( BorderFactory.createEmptyBorder ( 50, 50, 50, 50 ) );
            }
        } );
        dialog.pack ();
        dialog.setLocationRelativeTo ( frame );
        dialog.setVisible ( true );
    }
}

通过查看示例输出日志,您将看到打开 JDialog 时未传递来自 JFrame 的事件(甚至传递到示例中添加的全局 AWT 事件侦听器)。

所以我想知道 - 有没有办法捕捉到被阻止的 JFrame 上的点击?
或者至少在用户“触摸”被阻止的东西时捕捉到一个事件?

我需要这个的原因是在发生此类事件时使自定义装饰的 JDialog 闪烁。

4

2 回答 2

1

也许这有帮助:

如何在Java中获取我的窗口外的鼠标点击坐标

这有点困难,因为您要离开 Swing 领域并进入本机 GUI 领域。

于 2013-12-20T11:49:33.033 回答
1

如果你是模态的,我认为你不能这样做JDialog,但你可以使用下一个技巧FocusListener

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Example {
    public static void main(final String[] args) {

        final JFrame frame = new JFrame("Frame");
        final JDialog dialog = new JDialog(frame, "Dialog");
        frame.add(new JLabel("Content") {
            {
                setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
            }
        });
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent arg0) {
                System.out.println("frame pressed");
                System.out.println("dialog focused " + dialog.isFocused());
                System.out.println("frame focused " + frame.isFocused());
                super.mousePressed(arg0);
            }
        });
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        dialog.add(new JLabel("Content") {
            {
                setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
            }
        });
        dialog.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent arg0) {
                super.focusLost(arg0);
                dialog.requestFocus();
            }
        });
        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }
}

和输出:

frame pressed
dialog focused true
frame focused false
于 2013-12-20T11:53:59.993 回答