无法设置鼠标光标 JLayeredPane

发布于 2024-09-04 07:12:03 字数 826 浏览 9 评论 0原文

我遇到了一个问题,我似乎无法弄清楚也无法在网络上的任何地方找到答案。

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必你懂 2024-09-11 07:12:03

如果你看一下javax.swing.JLayeredPane源代码,你会看到它的构造函数是这样定义的:

public JLayeredPane() {
    setLayout(null);
}

它清楚地表明它需要自己处理组件布局。
因此您可以猜测(虽然没有记录,但我认为这是一个文档错误)您不应该更改 JLayeredPane 的布局。

If you take a look at javax.swing.JLayeredPane source code, you will see its constructor defined like that:

public JLayeredPane() {
    setLayout(null);
}

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.

长途伴 2024-09-11 07:12:03

虽然这个话题很老了,但没有一个答案令人满意。我通过以下方式解决了调用 JLayeredPane 的 setCursor 方法的问题:

this.getParent().setCursor( Cursor.getDefaultCursor() );

其中“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:

this.getParent().setCursor( Cursor.getDefaultCursor() );

Where "this" is the component I want to change the cursor to. Its parent is the JLayeredPane (since it is added to it).

萌吟 2024-09-11 07:12:03

使用演示代码时效果很好 如何使用分层窗格教程。

根据这 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.

失去的东西太少 2024-09-11 07:12:03

您是否尝试过获取第一个工作代码,但将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文