gpt4 book ai didi

java - 为什么 Swing JScrollPane 不响应鼠标滚轮?

转载 作者:行者123 更新时间:2023-12-02 04:49:05 24 4
gpt4 key购买 nike

我有以下 Swing UI 结构。

当我在蓝色 JPanel 中滚动鼠标滚轮时,JScrollPane 不响应鼠标滚轮事件。为什么?

我从 java 文档中读到:

JScrollPane provides a scrollable view of a lightweight component. ... Note that JScrollPane does not support heavyweight components.

所以这是因为我的结构太重了吗?还是有其他原因?

enter image description here

添加1

我不小心用鼠标中键单击切换了窗口。之后,鼠标滚轮突然对 JScrollPane 起作用了。

这让我认为这可能与焦点有关。然后我发现下面一行:

this.setFocusableWindowState(false);

将其更改为以下后,鼠标滚轮可以工作。

this.setFocusableWindowState(true);

尽管 javadoc 说:

Setting a Window's focusability state to false is the standard mechanism for an application to identify to the AWT a Window which will be used as a floating palette or toolbar, and thus should be a non-focusable Window.

首先,我猜测是因为JDialog不是处于可聚焦窗口状态,所以它无法接收事件。但实际上,鼠标点击总是有效的。所以我仍然不确定根本原因。

工具栏或 float 调色板似乎无法聚焦,但仍然可以接收鼠标单击事件。所以我猜想也许只有某些事件被setFocusableWindowState(false)过滤

最佳答案

关于重量级组件:AWT 组件是指使用操作系统小部件的组件。您使用 swing JComponents,J*。 Swing 组件被称为“轻量级”,因为它们在一个大型 native 窗口中模拟所有绘图和事件处理。

JPanel 应大于 JScrollPane,因此可以滚动“ View 端口”。正确设置首选尺寸

一般来说,我认为每个 JTextPane 都会位于其自己的 JScrollPane 中。

JScrollPane 的功能也有点不同。

scrollPane = new JScrollPane(panel);

关于java - 为什么 Swing JScrollPane 不响应鼠标滚轮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29409030/

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