Java: look and feel
What happens when I use NetBeans on a Windows machine is that if I run the main java file, I get a different look than when I run the whole program
I get
But if I do
I get
Do you see the look and feel of Java output? What I want there is that when I export it to jar, it should open as in the first case and look like a beautiful button What about?
Update 1: I found the following code at the beginning of the main:
public static void main(String args[]) { 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 ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,null,ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,ex); } catch (illegalaccessexception ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,ex); } //Some code here... }
So you should set the look and feel of nimbus, but the problem is that when I open the bottle, the look and feel of nimbus is not there
Update 2: look of my navigator
Update 3: file structure
Solution
When you run swing applications, the default "look and feel" setting is cross platform L & F, also known as metal On the other hand, when you create a new JFrame from the NetBeans new file wizard, it also includes the main methods for testing purposes only, enabling developers to "run" the top-level container In this main method, look & feel is set to nimbus, as included in your update 1
How can I change the default look and feel of JFrame? (not theme of NetBeans) As mentioned above, you can modify the template associated with the JFrame form to set the L & f you want, but note this line:
You can also view the article programmatically setting the look and feel
edit
A Java application must have only one main method to execute When you deploy the jar, the class with this main method is in manifest Defined in MF file Therefore, it is not a good habit to use a main method in each top-level container (JFrame or jdialog)
But sometimes you don't want to run the entire application to test how a particular frame / dialog works This is why NetBeans includes this main method in JFrame or jdialog creation But as mentioned above, when you deploy jars, you should remove these "extra" main methods
The swing application has a single JFrame and multiple jdialog For more information, please view this topic: the use of multiple jframes, good / bad practice?
You only need to programmatically set the main class (L & F executed when running the entire application) to nimbus You can copy and paste the code contained in update 1 there
Edit 2
When you create a new project in NetBeans, it asks you to create a main class Suppose I create a new project called test, which will require me to create such a main class:
The generated test class will have the main methods to trigger application execution:
In this main method, you must put the code you contain in update 1:
public class Test { public static void main(String[] args) { SwingUtilities.invokelater(new Runnable() { @Override public void run() { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { try { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } catch (ClassNotFoundException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } catch (InstantiationException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } catch (illegalaccessexception ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } } } } }); } }
Then, when you run the application, L & F will be set to nimbus to override the default cross platform L & F. from then on, all swing components created will use nimbus as L & F
Note: swingutilities The reason for calling invokelater() is explained in the initial threads article