我正在尝试更改JFrame
的装饰风格。我已经尝试过以下代码。
//...
static void main(String[] args) {
EventQueue.invokeLater(()-> {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("This is Title");
frame.setSize(500, 600);
frame.setVisible(true);
});
}
它改变了 JFrame
的装饰风格,但只有在外观设置为Metal时才有效。如果在运行时代码中某处的外观发生更改,如下所示:
for (UIManager.LookAndFeelInfo laf: UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName()) {
UIManager.setLookAndFeel(laf.getClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
}
然后,外观和感觉发生了变化,但框架
的装饰消失了。因此,我无法移动、调整大小、最大化、最小化和关闭框架
。
Question 1:
Is it possible to fix this problem?
Question 2:
Can I change the decoration style of the
JFrame
to make look like Visual Studio title bar?
最佳答案
您可以将 JFrame 实例本身设置为未修饰:
static void main(String[] args) {
EventQueue.invokeLater(()-> {
JFrame frame = new JFrame("This is Title");
frame.setUndecorated(true);
frame.setSize(500, 600);
frame.setVisible(true);
});
}
如果您希望它以不同的方式装饰(而不是未装饰),您必须编写自己的 LookAndFeel。
或者尝试使用“SystemLookAndFeel” - 这大致看起来像您正在运行的操作系统。如果是 Windows,它最终可能看起来有点像 Visual Studio...
关于java - 改变Java Swing JFrame的装饰风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49938320/