[英]How to set jframe look and feel
我有点困惑在哪里把这个:
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e){
}
我没有扩展JFrame
类但使用了JFrame f = new JFrame();
感谢:D
注意:这不是问题的答案(在哪里设置LAF)。 相反,它正在回答如何以独立于其包名的方式设置LAF的问题。 在类被移动的情况下简化生活,如从com.sun *到javax.swing的fi Nimbus。
基本方法是查询UIManager以获取其安装的LAF,循环遍历它们直到找到匹配并设置它。 这里有在SwingX中实现的方法:
/**
* Returns the class name of the installed LookAndFeel with a name
* containing the name snippet or null if none found.
*
* @param nameSnippet a snippet contained in the Laf's name
* @return the class name if installed, or null
*/
public static String getLookAndFeelClassName(String nameSnippet) {
LookAndFeelInfo[] plafs = UIManager.getInstalledLookAndFeels();
for (LookAndFeelInfo info : plafs) {
if (info.getName().contains(nameSnippet)) {
return info.getClassName();
}
}
return null;
}
用法(此处无异常处理)
String className = getLookAndFeelClassName("Nimbus");
UIManager.setLookAndFeel(className);
放置它的最常见的地方就在你的静态void main(String [] args)方法中。 像这样:
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception ignored){}
new YourClass(); //start your application
}
欲了解更多信息,请访问该网站: http : //docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
UIManager.setLookAndFeel()
不适用于已创建的组件。 这是为应用程序中的每个窗口设置外观的好方法。 这将在程序中的所有打开的Windows上设置它。 创建的任何新窗口都将使用UIManager设置的内容。
UIManager.setLookAndFeel(lookModel.getLookAndFeels().get(getLookAndFeel()));
for(Window window : JFrame.getWindows()) {
SwingUtilities.updateComponentTreeUI(window);
}
您可以在创建JFrame之后将此块放在main方法中,或者在扩展JFrame的类的构造函数中放置此块。
try
{
//Set the required look and feel
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
//Update the component tree - associate the look and feel with the given frame.
SwingUtilities.updateComponentTreeUI(frame);
}//end try
catch(Exception ex)
{
ex.printStackTrace();
}//end catch
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException || javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger( Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.