问题
I am just learning Java and still have not been able to sort this little problem I have
My pop up Calendar uses Nimbus look and feel but I have panels and container Jtables that use Java's look and feel - I am trying to make every GUI screen/ window use the nimbus look and feel and it was suggested by Merky to put the foolowing code in my main to make every subsequent screen have the Nimbus look and feel but I cannot get it to work so can someone tell me where and how I should put this code PLEASE.
public static void main(String args[])
{
SA md=new OptraderSA("Copyright© 2010 Simon Andi");
Dimension sd=Toolkit.getDefaultToolkit().getScreenSize();
md.setLocation(sd.width/2-400/2, sd.height/2-400/2);
md.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*Suggested Code*/
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
System.out.println("CHOSEN THIS");
break;
}
else{
UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
}
} catch (Exception e) {
/ If Nimbus is not available, you can set to another look and feel.
Cant get it to compile or work.
}
Would apprciate some help Please Simon
回答1:
This is what I do in my main method to enable Nimbus L&F.
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, fall back to cross-platform
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ex) {
// not worth my time
}
}
new Controller();
}
You need to be sure to configure the UIManager with the Nimbus L&F before you start the swing event dispatch thread (before calling view.setVisible(true)). Hope that helps.
回答2:
I think try with :
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
System.out.println("CHOSEN THIS");
break;
}
回答3:
Take a look at these:
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
回答4:
To set Nimbus look and feel add this code in your main method
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/4617615/how-to-set-nimbus-look-and-feel-in-main