对于玻璃板的目的,为什么Swing中的输入元素似乎不被视为JPanel的一部分? [英] For glasspane purposes, why are input elements in Swing seemingly not considered part of a JPanel?

查看:94
本文介绍了对于玻璃板的目的,为什么Swing中的输入元素似乎不被视为JPanel的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

By input elements I mean things like JSpinners and JComboxBoxes. My glasspane is passed a JPanel containing JSpinners, JComboBoxes and for the most part, JLabels. The glasspane has a MouseListener attached. The surprising thing is that mouseEntered is called upon the mouse cursor leaving the input elements and hovering over the other parts or empty space of the JPanel! Is this normal behaviour? How can I get the input elements to be considered part of the JPanel for Glasspane purposes?

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

Here is a screenshot of my UI with its input elements and jLabels.

这是一个示例代码:

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在离开输入元素时似乎正在生成新事件,我可能在实现这一点时遇到一些困难。

By way of explanation, my goal is to eventually use the GlassPane to block input for those elements marked with the prohibition sign. However, given that the mouseListener assigned to the dialogPanel is seemingly generating new events upon leaving the input elements, I may have some difficulties achieving this.

推荐答案

您可以将鼠标事件转发到基础组件,如 Glass Pane 演示方法, redispatchMouseEvent()

You can forward mouse events to the underlying components, as shown in The Glass Pane demo's method, redispatchMouseEvent().

这篇关于对于玻璃板的目的,为什么Swing中的输入元素似乎不被视为JPanel的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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