Domanda

My java program has multiple classes, each containing a separate JFrame. All three JFrames are started in my main class, and are set to setVisible(false) so they are hidden. When a button from a JPopUpMenu is clicked, the respective JFrame is set to setVisible(true). In these classes I have defined:

 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

When I run each class separately by right-clicking the .java file in NetBeans and choosing Run File, the Look and Feel works perfectly. However, when I run the main class and setVisible(true) on the JFrame, the look and feel does not work. I thought this may have been just an error with NetBeans, so I tried building the .jar and running it. The problem is still there. The main class's Look and Feel has been set using the same code, and that works perfectly. I am not sure what to do. Any help is appreciated.

È stato utile?

Soluzione

From the JavaDocs of UIManager:

Once the look and feel has been changed it is imperative to invoke updateUI on all JComponents. The method SwingUtilities.updateComponentTreeUI(java.awt.Component) makes it easy to apply updateUI to a containment hierarchy. Refer to it for details. The exact behavior of not invoking updateUI after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.

If you change the look and feel after creating your frames (even if they're not yet visible) then you need to updateUI for the change to take effect (or better, if you know you're always going to want to use a specific look and feel then set that in your main class before you create the frames in the first place).

Altri suggerimenti

Yes. Same problem. If set LookAndFeel BEFORE create form, all works ) if set after creation, it may be not work )

Installed LookAndFeel can be show

        LookAndFeelInfo [] lf = UIManager.getInstalledLookAndFeels();
        for(int n = 0; n < lf.length; n++) {
            System.out.println(lf[n].getClassName());
        }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top