无法设置鼠标光标 JLayeredPane
我遇到了一个问题,我似乎无法弄清楚也无法在网络上的任何地方找到答案。
我有一个 JLayeredPane,当它只有一个子面板时,我可以使用 setCursor() 正确设置光标。光标出现,一切正常。但是,当我将额外的 JPanel 添加到 JLayeredPane 中时,光标不再显示,
例如,这有效:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
// m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // WORKS
但这不起作用:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // FAILS
任何人都知道如何让自定义光标在 JLayeredPane 中工作
I'm running into a problem that I can't seem to figure out nor find the answer anywhere on the web.
I've got a JLayeredPane and when it only has one child Panel I am able to correctly set the cursor using setCursor(). The cursor shows up and everything is fine. But when I add an additional JPanel into the JLayeredPane the cursor no longer shows up
for example this works:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
// m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // WORKS
but this doesn't:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // FAILS
Anyone know how i can get custom cursors working within a JLayeredPane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你看一下
javax.swing.JLayeredPane
源代码,你会看到它的构造函数是这样定义的:它清楚地表明它需要自己处理组件布局。
因此您可以猜测(虽然没有记录,但我认为这是一个文档错误)您不应该更改 JLayeredPane 的布局。
If you take a look at
javax.swing.JLayeredPane
source code, you will see its constructor defined like that:which clearly indicates that it needs to handle components layout by itself.
Hence you can guess (although it is not documented, I would consider it a documentation bug) that you should not change the layout of
JLayeredPane
.虽然这个话题很老了,但没有一个答案令人满意。我通过以下方式解决了调用 JLayeredPane 的 setCursor 方法的问题:
其中“this”是我想要将光标更改为的组件。它的父级是 JLayeredPane(因为它被添加到其中)。
While the topic is old, none of the answers was satisfying. I resolved the problem calling to the setCursor method of the JLayeredPane in this way:
Where "this" is the component I want to change the cursor to. Its parent is the JLayeredPane (since it is added to it).
使用演示代码时效果很好 如何使用分层窗格教程。
根据这 3 行代码,我从教程中看到的唯一区别是您正在使用布局管理器。
将您的代码与教程进行比较以发现其他差异。
Works fine for me when using the demo code the the How to Use Layered Panes tutorial.
Based on the 3 lines of code the only difference I can see from the tutorial is that you are using a layout manager.
Compare your code with the tutorial to find other differences.
您是否尝试过获取第一个工作代码,但将 m_mediaPanel 放在级别 1 上?这可能也行不通。我认为这是因为顶部的面板决定了光标。在级别 0 上,分层窗格本身可以确定这一点。
Have you tried taking the first working code, but placing the m_mediaPanel on level 1? This probably won't work either. I think this is due to the fact that the panel that is on top determines the cursor. On level 0 the layered pane itself can determine this.