java - 如何删除窗口调整大小时的黑色矩形?

标签 java swing jframe resize window

我正在使用 Swing 和 AWT 开发 JFrame 窗口,当我调整窗口大小时,它看起来像这样:

enter image description here

正如您所看到的,很多时候当我调整窗口大小时,它会显示一个正在调整大小的黑色矩形,并且直到您暂停一会儿它才会消失。此外,圆圈并不总是随着我的调整大小事件而准确更新:

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/

相关文章:

java - 从另一个 JPanel 类更改 JFrames 面板

JAVA - 从不同类别获取随机数

java - 在Android应用程序中不要硬编码的内容

java - 如何在Java中为多个用户实现多问题轮询线程?

java - JButton 错误无法应用于给定类型

java - 如何获取JFrame中的所有元素?

java - 没有视觉出现

java - 如何向特定 Midi 端口发送 Midi 消息

Java Applet 代码可以在浏览器中运行,但不能在 AppletViewer 中运行

Java - 水平重复渐变图像(Swing)