Changing look and feel of specific window

前端 未结 4 1005
你的背包
你的背包 2020-12-07 03:54

I\'m writing a script for a larger GUI application. The main application window uses the system\'s LookAndFeel, but I want my script\'s GUI to use the Nimbus

4条回答
  •  执笔经年
    2020-12-07 04:47

    The problem originates from attempting to do the PLAF change in a static block. Move it to the constructor and it works.

    import java.awt.Dimension;
    import java.awt.GridBagLayout;
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    
    public class GUI extends JFrame {
        private static LookAndFeel originalLookAndFeel = UIManager.getLookAndFeel();
        private GridBagLayout gridBag = new GridBagLayout();
        private JTabbedPane tabs = new JTabbedPane();
        private JPanel selectionPanel = new JPanel(gridBag);
        private JPanel infoPanel = new JPanel(gridBag);
        private JPanel settingsPanel = new JPanel(gridBag);
    
        public GUI() {
            System.out.println("At start, look and feel is " + UIManager.getLookAndFeel().getName());
            try {
                setNimbusLookAndFeel();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Look and feel changed to " + UIManager.getLookAndFeel().getName()
                    + " before component creation");
    
            setWindowProperties();
            setUpComponents();
            addComponents();
    
            try {
                System.out.println("Setting to original, which is " + originalLookAndFeel.getName());
                UIManager.setLookAndFeel(originalLookAndFeel);
                System.out.println("Current look and feel is " + UIManager.getLookAndFeel().getName());
            } catch (UnsupportedLookAndFeelException e) {
                //e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
    
        private void setWindowProperties() {
            setLayout(gridBag);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(new Dimension(700, 600));
            setTitle("fAmos Quester");
            setResizable(false);
            setLocationRelativeTo(null);
        }
    
        private void setNimbusLookAndFeel() {
            try {
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                    }
                }
            } catch (Exception e) {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e2) {
                }
            }
        }
    
        public void setUpComponents() {
            tabs.addTab("Quest selection", selectionPanel);
            tabs.addTab("Quest info", infoPanel);
            tabs.addTab("Settings", settingsPanel);
    
            selectionPanel.setPreferredSize(new Dimension(650, 500));
            infoPanel.setPreferredSize(new Dimension(650, 500));
            settingsPanel.setPreferredSize(new Dimension(650, 500));
        }
    
        private void addComponents() {
            add(tabs);
        }
    
        public static void main(String[] args) {
            new GUI().setVisible(true);
        }
    }
    

提交回复
热议问题