gpt4 book ai didi

java - MousExited 不会在带有 GlassPane 的 JDialog 上被触发,而 MouseEntered 会被触发

转载 作者:行者123 更新时间:2023-12-01 14:38:06 25 4
gpt4 key购买 nike

我制作了一个自己的Window-Model,它扩展了JFrame和JDialog,没有Window-Decoration。如果我创建一个新窗口,它就是相应模型的实例。为了处理这些窗口的大小调整,我添加了 well behaved glass pane到这些模型。 GlassPane 像这样添加到框架/对话框中

//here, I create a new GlassPane and give it the instance of the JFrame/JDialog, so the
AWT-Listener in the GlassPane can resize this instance.
glassPane = new GlassPane(this, res, valWidth, valHeight, RESIZEBORDER,
BORDERPIXEL, isResizable, keepRatio);
//then I set this GlassPane to the JFame/JDialog
this.setGlassPane(glassPane);

//this is from the example. I'm not absolutly sure, what it does.
if (this.glassPane instanceof AWTEventListener) {
AWTEventListener al = (AWTEventListener) glassPane;
Toolkit.getDefaultToolkit().addAWTEventListener(
al,
AWTEvent.MOUSE_MOTION_EVENT_MASK
| AWTEvent.MOUSE_EVENT_MASK);
}
glassPane.setVisible(true);
glassPane.repaint();

在 GlassPane 中,我修改了示例中的事件,如下所示:

public void eventDispatched(AWTEvent event) {
if (event instanceof MouseEvent) {


//I convert the MouseEvent like in the example
MouseEvent e = (MouseEvent)event;

if (target instanceof JFrame) {
e = SwingUtilities.convertMouseEvent(
((MouseEvent) event).getComponent(),
(MouseEvent) event, ((JFrame) target).getGlassPane());
} else if (target instanceof JDialog) {
e = SwingUtilities.convertMouseEvent(
((MouseEvent) event).getComponent(),
(MouseEvent) event, this);
}

if (!SwingUtilities.isDescendingFrom(e.getComponent(), target)) {
return;

}
//I check the events like this
if (e.getID() == MouseEvent.MOUSE_EXITED) {
System.out.println("exit");
}

现在我的问题:

如果我打开一个 JFrame 并且该框架打开一个 JDialog,则所有大小调整都会起作用。如果我将光标移到 JDialog 上,MOUSE_ENTERS-Event 也会被触发。但是,只有当我将光标移出 JDialog 并移出 JFrame 时,才会触发 MOUSE_EXITED-Event 。另一个问题是,Mouse_Entered-Event 被触发两次:第一个由 JDialog 的 GlassPane 触发,第二个由 JFrame 的 GlassPane 触发。我还尝试将 MouseListener 直接添加到 JFrame/JDialog,但这具有相同的效果。也许这很重要:我已将 JFrame 设置为 JDialog 的所有者。

我不知道为什么会这样..我希望有人可以帮助我。

提前致谢。

最佳答案

To handle resizing of these windows...

参见Resizing Components寻找可能更简单的解决方案。

关于java - MousExited 不会在带有 GlassPane 的 JDialog 上被触发,而 MouseEntered 会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com