gpt4 book ai didi

java - setUndecorated 不适用于非默认外观和感觉

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:50 24 4
gpt4 key购买 nike

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Frame {

private JFrame jFrame;

public Frame() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}

private void create() {
jFrame = new JFrame("frame");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(200, 200);
jFrame.setVisible(true);
}

public static void main(String[] args) {
new Frame().create();
}

}

上面的代码工作正常,但如果我将 jFrame.undecorated 设置为 true,它不会删除框架?有谁知道为什么不?谢谢。

编辑:还发现如果我将 jFrame.undecorated 设置为 false,还会显示另一个具有默认外观的框架。像这样:

example

最佳答案

检查有关 setUndecorated() 方法的文档 - 只能在不可见时调用。您的代码在构造函数中注释掉了两个调用,但添加了 jFrame.setUndecorated(true);在 setVisible() 调用之前。

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Frame {

private JFrame jFrame;

public Frame() {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
//JFrame.setDefaultLookAndFeelDecorated(true);
//JDialog.setDefaultLookAndFeelDecorated(true);

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}

private void create() {
jFrame = new JFrame("frame");
jFrame.setUndecorated(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(200, 200);
jFrame.setVisible(true);

}

public static void main(String[] args) {
new Frame().create();
}

}

关于java - setUndecorated 不适用于非默认外观和感觉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837328/

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