gpt4 book ai didi

java - 出于 glasspane 的目的,为什么 Swing 中的输入元素似乎不被视为 JPanel 的一部分?

转载 作者:行者123 更新时间:2023-12-03 02:41:38 24 4
gpt4 key购买 nike

我所说的输入元素是指 JSpinners 和 JComboxBoxes 等元素。我的 glasspane 传递了一个包含 JSpinners、JComboBoxes 以及大部分 JLabels 的 JPanel。玻璃板附有一个 MouseListener。令人惊讶的是,当鼠标光标离开输入元素并悬停在 JPanel 的其他部分或空白区域上时,将调用 mouseEntered!这是正常行为吗?出于 Glasspane 的目的,如何才能将输入元素视为 JPanel 的一部分?

这是我的 UI 及其输入元素和 jLabels 的屏幕截图。 Sample UI

这是一段示例代码:

import javax.swing.*;

public class DialogTest {
public DialogTest() {
JPanel dialogPanel = new JPanel();
SpinnerModel edgeModel = new SpinnerNumberModel(1, 1, 9, 1);
JSpinner edgeSpn = new JSpinner(edgeModel);
dialogPanel.add(edgeSpn);

JDialog initialDialog = new JDialog(new JFrame(), "Test", true);
initialDialog.setContentPane(dialogPanel);
initialDialog.pack();
glass = new GlassComponent(dialogPanel);
initialDialog.setGlassPane(glass);
glass.setOpaque(false);
glass.setVisible(true);
initialDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
initialDialog.setVisible(true);
}
}

public class GlassComponent implements MouseListener {
JPanel c;
public GlassComponent(JPanel c) {
this.c = c;
this.c.addMouseListener(this);
}

...
public mouseEntered(MouseEvent e) {
System.out.println("Entered JPanel");
}
}

作为解释,我的目标是最终使用 GlassPane 来阻止那些标有禁止标志的元素的输入。然而,考虑到分配给dialogPanel的mouseListener似乎在离开输入元素时生成新事件,我可能很难实现这一点。

最佳答案

可以将鼠标事件转发到底层组件,如The Glass Pane所示演示的方法,redispatchMouseEvent()

关于java - 出于 glasspane 的目的,为什么 Swing 中的输入元素似乎不被视为 JPanel 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707470/

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