java JFrame 和 Nimbus 外观和感觉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7612592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JFrame and Nimbus Look And Feel
提问by MOD
I use Nimbus Look and Feel in a project. However, although every GUI JComponent have a Look and Feel of Nimbus, JFrame always have Windows Look and Feel.
我在一个项目中使用 Nimbus 外观和感觉。但是,尽管每个 GUI JComponent 都有 Nimbus 的外观,但 JFrame 始终具有 Windows 外观。
How can JFrame have Nimbus Look And Feel?
JFrame 如何拥有 Nimbus 的外观和感觉?
Edit: Operating System : Windows XP
编辑:操作系统:Windows XP
回答by Marek Sebera
Try using this:
尝试使用这个:
JFrame.setDefaultLookAndFeelDecorated(true); //before creating JFrames
For more info., see How to Set the Look and Feelin the tutorial.
有关详细信息,请参阅教程中的如何设置外观。
import javax.swing.*;
class FrameLook {
public static void showFrame(String plaf) {
try {
UIManager.setLookAndFeel(plaf);
} catch(Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame(plaf);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(400,100);
f.setLocationByPlatform(true);
f.setDefaultLookAndFeelDecorated(true);
f.setVisible(true);
}
public static void main(String[] args) {
showFrame(UIManager.getSystemLookAndFeelClassName());
showFrame(UIManager.getCrossPlatformLookAndFeelClassName());
showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
}
回答by trashgod
Confirming @Andrew's suspicion, setDefaultLookAndFeelDecorated()
says that, when supported, "newly created JFrame
s will have their Window
decorations provided by the current LookAndFeel
." I changed the size to see the whole title.
确认@Andrew 的怀疑,setDefaultLookAndFeelDecorated()
他说,如果得到支持,“新创建的JFrame
s 将由Window
当前的s提供装饰LookAndFeel
。” 我更改了大小以查看整个标题。
import javax.swing.*;
class FrameLook {
public static void showFrame(String plaf) {
try {
UIManager.setLookAndFeel(plaf);
} catch (Exception e) {
e.printStackTrace(System.out);
}
JFrame f = new JFrame(plaf);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setSize(500, 100);
f.setLocationByPlatform(true);
JFrame.setDefaultLookAndFeelDecorated(true);
f.setVisible(true);
}
public static void main(String[] args) {
showFrame(UIManager.getSystemLookAndFeelClassName());
showFrame(UIManager.getCrossPlatformLookAndFeelClassName());
showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
}
回答by Spencer Kormos
And confirming based on the Windows Classic UI for XP.
并基于 XP 的 Windows 经典 UI 进行确认。
回答by Yago Méndez Vidal
You can't do it directly since Nimbus doesn't support window decorations, that's why you always get a system window, even with the given answers. Try this very simple code:
你不能直接这样做,因为 Nimbus 不支持窗口装饰,这就是为什么你总是得到一个系统窗口,即使有给定的答案。试试这个非常简单的代码:
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
public class DoesNimbusSupportWindowDecorations {
@SuppressWarnings("unchecked")
public static void main(String... args) {
LookAndFeel nimbus = null;
for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
if (lafInfo.getName() == "Nimbus") {
try {
nimbus = ((Class<LookAndFeel>) Class.forName(
lafInfo.getClassName())).newInstance();
} catch (Exception e) {
System.err.println("Unexpected exception.");
}
}
}
if (nimbus != null) {
System.out.println("Nimbus supports window decorations...? "
+ (nimbus.getSupportsWindowDecorations() ? "YES" : "NO"));
} else {
System.err.println("Your system does not support Nimbus, you can't"
+ " run this test.");
}
}
}
or simply inside your code with the proper import:
或者只是在您的代码中使用正确的导入:
System.out.println(new NimbusLookAndFeel().getSupportsWindowDecorations());
What's beyond my understanding is why Sun decided such a thing since the decorations do exist for internal frames and have a custom decoration. I'll be investigating if it's possible to use these decorations by extending NimbusLookAndFeel or playing with defaults, since Nimbus is based on Synth, unsure about the best way.
令我无法理解的是,Sun 为何会做出这样的决定,因为内部框架确实存在装饰并且有定制的装饰。我将调查是否可以通过扩展 NimbusLookAndFeel 或使用默认值来使用这些装饰,因为 Nimbus 基于 Synth,不确定最佳方式。
回答by Tan90
This is how I do. A copy paste from my eclipse project.
这就是我的做法。从我的 eclipse 项目复制粘贴。
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.*;
public class Frame1 {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
Frame1 window = new Frame1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}