我正在使用 Swing 和 AWT 开发 JFrame 窗口,当我调整窗口大小时,它看起来像这样:
正如您所看到的,很多时候当我调整窗口大小时,它会显示一个正在调整大小的黑色矩形,并且直到您暂停一会儿它才会消失。此外,圆圈并不总是随着我的调整大小事件而准确更新:
frame.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent componentEvent) {
width = frame.getWidth();
height = frame.getHeight();
springLayout.putConstraint(SpringLayout.SOUTH, panel, height, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, panel, width, SpringLayout.WEST, frame.getContentPane());
panel.repaint();
}
});
这不是重点(尽管这两个问题可能是相关的)。我只在使用Swing时遇到过这个问题。 JavaFX从来没有给我这个问题。 有什么办法可以在调整窗口大小时删除黑色矩形吗?
最佳答案
我只在 Windows 10 上测试过此功能。
这显然与托管 JFrame 的窗口的 native 窗口装饰有关。禁用 native 窗口装饰时,您可以删除断断续续的大小调整和渗透的黑色背景。请参阅 JFrame.setDefaultLookAndFeelDecolated(boolean)
的文档:
Provides a hint as to whether or not newly created JFrames should have their Window decorations (such as borders, widgets to close the window, title...) provided by the current look and feel. If defaultLookAndFeelDecorated is true, the current LookAndFeel supports providing window decorations, and the current window manager supports undecorated windows, then newly created JFrames will have their Window decorations provided by the current LookAndFeel. Otherwise, newly created JFrames will have their Window decorations provided by the current window manager.
因此,您有两个选择。在创建 JFrame 之前设置该属性一次:
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
或者在创建 JFrame 之后执行此操作:
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setWindowDecorationStyle(JRootPane.FRAME);
请注意,无论哪种情况,您都必须使用 Swing 的外观和感觉来进行窗口装饰。因此,窗口的标题栏和 handle 看起来将与以前不同。
关于java - 如何删除窗口调整大小时的黑色矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55987389/