java - Linux 上的 JDialog:当 Type 为 Type.POPUP 时,组件可单击但不可见
问题描述
正如标题所述,我有一个带有不可见但可点击的组件的 JDialog。请参阅下面的 SSCCE。这在带有 oracle jdk1.8.0_201 的 Windows 上表现正确(即“关闭我”按钮可见)。使用 oracle jdk1.8.0_201 在带有 openbox 的 Debian 10 上可以重现该问题。
当问题发生时,JDialog 会出现,但完全是灰色的。该按钮虽然是可点击的,但确实会按预期关闭对话框。
当我没有将类型设置为“POPUP”并且在示例代码中注释了其他修改时,问题似乎消失了(改变发生率)。我想保留 POPUP 类型,因为设置它解决了在实际应用程序的上下文中难以摆脱的其他问题。
我究竟做错了什么?
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
public class LockPane extends JDialog {
public LockPane(JFrame parentFrame, JPanel componentToShow, Dimension paneSize) {
super(parentFrame, true);
setType(Type.POPUP); // removing this seems to fix the issue. but why?
setUndecorated(true); // removing this seems to make the issue less likely to occur but does not fix it completely
setSize(paneSize); // removing this seems to make the issue less likely to occur but does not fix it completely
setPreferredSize(paneSize);
setMaximumSize(paneSize); // removing this seems to make the issue less likely to occur but does not fix it completely
setLayout(new GridLayout(1, 1));
add(componentToShow);
setAlwaysOnTop(true);
pack();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Lockpane Demo");
frame.setLayout(new BorderLayout());
JButton button = new JButton("click me to demonstrate the problem");
button.setPreferredSize(new Dimension(400, 400));
frame.add(button);
frame.pack();
button.addActionListener(e -> {
JButton closeButton = new JButton("Close me");
JPanel componentToShow = new JPanel(new BorderLayout());
componentToShow.setSize(new Dimension(200, 200));
LockPane lockPane = new LockPane(frame, componentToShow, new Dimension(200, 200));
componentToShow.add(closeButton, BorderLayout.CENTER);
lockPane.add(componentToShow);
closeButton.addActionListener(e1 -> {
System.out.println("lock pane button was clicked");
lockPane.setVisible(false);
});
lockPane.setVisible(true);
});
frame.setVisible(true);
}
}
编辑:我用 openjdk-11 测试了这个 - 在那里无法重现!然后我用adoptopenjdk jdk8u242-b08测试,问题又出现了。这实际上可能是 Java 8 中的错误吗?
EDIT2:我在 Debian 10 上使用带有 Cinnamon 桌面的采用openjdk jdk8u242-b08 对此进行了测试。同样的问题。所以我认为它与桌面无关。
EDIT3:我放弃了。我将不使用 Window Type Popup。如果有人找到解决方案,我仍然会感兴趣。
解决方案
推荐阅读
- spring-boot - Spring Boot RestController + Jackson:将 JSONObject 数组反序列化为 Map
- java - 检测类类型并重定向到正确的方法
- python - DRF 中的异步请求错误
- django - Django 多对多字段查询
- java - OptaPlanner 中无法解释的 NPE
- angular - 无法在角度 4 中隐藏错误消息
- c# - 使用适用于桌面应用程序 (WPF) 的 Azure AD 实施 SAML SSO
- python - 使用python从文本中提取具有不同格式的所有日期
- r - 如何使用 knn 在 r 中正确使用函数预测?
- javascript - jQuery找到给定类的第一个以下元素?