- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个多层 java 应用程序,其中包含一系列需要手形光标的 JLabel 按钮,例如:
button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
但是,我上面有一个层,其边界覆盖整个应用程序(用于绘制弹出窗口等)。现在我已经添加了上面的图层,我的光标不再改变。我尝试将上面图层的光标设置为空,但没有任何影响。
这是图层的基本布局:
private void create() {
this.addWindowListener(windowAdapter);
this.setLayout(new BorderLayout());
layers = new JLayeredPane();
layers.setPreferredSize(this.getSize());
dashboard = new DashBoard.DashBoardLayer();
dashboard.setBounds(0, this.getHeight()-275, this.getWidth(),275);
application = new App.ApplicationLayer();
application.setBounds(0,0,this.getWidth(),this.getHeight()-145);
filter = new FilterLayer();
filter.setBounds(0,195,this.getWidth(),490);
menuBG = MenuLayerBg.getInstance();
menuBG.setBounds(0,0,this.getWidth(),this.getHeight());
menuPanes = MenuLayer.getInstance();
menuPanes.setBounds(0,0,this.getWidth(),this.getHeight());
layers.add(application, new Integer(0));
layers.add(filter, new Integer(1));
layers.add(dashboard, new Integer(2));
layers.add(menuBG, new Integer(3));
layers.add(menuPanes, new Integer(4));
this.add(layers, BorderLayout.CENTER);
}
最佳答案
MouseEvent 仅被分派(dispatch)到顶部的组件。因此,如果顶层完全覆盖底层的组件,它们将不会收到 MouseEvent。
查看 How to Use Layered Panes 上 Swing 教程中的 LayeredPaneDemo.java
代码。我对代码进行了以下更改:
for (int i = 0; i < layerStrings.length; i++) {
JLabel label = createColoredLabel(layerStrings[i],
layerColors[i], origin);
layeredPane.add(label, new Integer(i));
origin.x += offset;
origin.y += offset;
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // added
}
每个组件的大小固定小于整个框架,因此事件将被顶部组件下方的组件接受。
当您运行代码时,光标将在顶部两个标签上发生变化。
如果将“Dukes Layer and Position”更改为“Yellow”并取消选中该复选框,则所有标签的光标都会发生变化。
However, I have a layer above that has bounds covering the entire application
一种可能的解决方案是将鼠标事件“重新分派(dispatch)”到顶层以下的组件。有关此方法的示例,请查看 How to Use Root Panes 上 Swing 教程中的 GlassPaneDemo.java
或者对于完全不同的方法,您可以尝试重写组件的 contains(...)
方法以始终返回 false。这样,鼠标事件将永远不会被分派(dispatch)到您的组件,因为鼠标点不属于该组件。我以前从未尝试过,所以我不知道这是否会导致其他问题。
我在 LayeredPaneDemo 中尝试了这一点,将以下内容更改为 createColoredLabel(...)
方法:
JLabel label = null;
if (color.equals(Color.green))
{
label = new JLabel(text)
{
@Override
public boolean contains(int x, int y)
{
return false;
}
};
}
else
label = new JLabel(text);
//JLabel label = new JLabel(text);
关于Java JLayeredPane 覆盖光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24070864/
我正在尝试创建一个带有 JLayeredPane 的应用程序,该应用程序会根据父 JFrame 的大小自动缩放(这是使用 BorderLayout 作为框架内容 Pane 上的布局管理器)。困难的部分
是否可以在JTable内添加JLayeredPane()? 我尝试过: JLayeredPane agentPicturePane = new JLayeredPane(); St
我一直在努力做一些我认为很简单的事情: 我想创建一个带有背景图像的表单(JTextField)。为了使表单不覆盖背景图像,我使用 JLayeredPane。我一直在尝试不同的东西,似乎没有任何效果:出
我正在使用 JLayeredPane 来定位我的 JPanel 3 维,这样当有人单击 jpanel 时我可以实现“缩放”效果,我需要动态更改图层索引,以便单击的组件始终是在上面”。 JPanel j
我有一个 JLayeredPane。我在上面放置了几个组件。这些组件是扩展 jPanel 的类。所有组件都放置在同一深度层中。每个组件可能有也可能没有完全/部分位于其上方/下方的其他组件。当我单击组件
我有以下问题:我有一个 JLayeredPane,我需要向其中添加一个圆和一些线条。线条的问题是,它们应该能够重绘/移动位置(线条象征一些移动 vector )当我只是将一些 GLine 添加到 Gr
我有一个有两层的 JLayeredPane。顶层是完全透明的,充当玻璃 Pane ,捕获鼠标事件,然后将它们分派(dispatch)到其下方的 Pane 。由于未知原因,底层未正确绘制。我是否必须向其
我正在尝试创建一个面板,该面板使用 JLayeredPane 使面板位于另一个更大的面板上方的中心。但我似乎无法显示较小的面板。关于我做错了什么有什么想法吗? import java.awt.Dime
我有一个多层 java 应用程序,其中包含一系列需要手形光标的 JLabel 按钮,例如: button.setCursor(Cursor.getPredefinedCursor(Cursor.HAN
我有一个从 JFrame 扩展的类。我里面有多个 JLayeredPanes。我想从一个 JLayeredPane 切换到另一个。为此,我所做的是; 删除当前的 JLayeredPane,如 remo
我在 NetBeans 中有一个项目,它使用分层 Pane 通过在必要时在它们之间切换来显示重叠的面板。启动时,程序会在其中一个面板上显示介绍动画(在单独的线程上运行),通过以下方式实现: publi
我在 JLayeredPane 上有一些按钮和面板。 当我点击一个按钮时,一个面板通过 setvisible(true) 显示。但是,如果面板后面有一个按钮,并且我将鼠标悬停在按钮上,按钮就会出现在面
我的 JLayeredPane 有问题,我可能正在做一些非常简单的事情,但我无法全神贯注。我遇到的问题是所有组件都合并在一起并且没有排序。你能纠正这个吗,因为我不知道。我尝试执行的顺序是这样的布局 输
我有一个图像,我想要的只是将此图像打包到 JLabel 中,并将其添加到我的 JLayeredPane 中,以便稍后对多个图像进行分层。它可以在没有 JLayeredPane 的情况下处理一张图像,但
我想在运行时动态添加/隐藏/删除 jlayeredpanes,并且在选择另一个 Pane 时还应该隐藏每个 Pane 上的内容。我已经尝试过以下代码,但我不知道如何执行此操作。以下代码在选择备用 Pa
我有一个代码,可以在鼠标单击时在 jLayeredpane 中的单击位置添加 jbutton。现在问题出在调整大小上。当我在运行时不更改 jLayeredpane 的大小时,就没有问题。当我最大化窗口
我在分层 Pane 中有一组面板。我需要一个分隔符来将 sideBar 与 topBar 和 tabbedPanel 分开。我留了一个 10 像素的缓冲区来放置它。不幸的是,可能由于它是 JLayer
我需要一个 UI,我想在其中描绘网络设备的图形表示。为此,我只是加载多个图像并将它们一一重叠以显示设备的当前状态。我必须支持此 View 的缩放。缩放代码如下所示。 public void zoom(
在这里尝试分层 Pane ,但遇到了问题。我有两个 JPanel。我将它们都添加到 JLayeredPane 中 - panel1 足够大,可以占据整个空间,而 panel2 较小,并且使用 Flow
我正在尝试使用 JLayeredPane 将一个 JPanel 覆盖在另一个 JPanel 之上。由于某种原因,添加的面板没有显示。 这是创建 JLayeredPane 并向其中添加元素的代码: im
我是一名优秀的程序员,十分优秀!