为什么外观和感觉没有得到正确的更新?

时间:2010-05-04 06:12:08

标签: java swing look-and-feel

我正在开发一个swing应用程序,我可以选择通过单击按钮来更改应用程序的外观。现在我的问题是,当我点击按钮更改主题时,它没有正确更新我的应用程序的L& F,说我之前的主题是“noire”,我之后选择“MCWin”,但是noire主题的风格是还在那里

以下是示例工作代码:

package whiteboard;

    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.WindowConstants;



    public class DiscussionBoard extends JFrame implements ComponentListener,ActionListener {   

        // Variables declaration
        private JMenuItem audioMenuItem;
        private JMenuItem boardMenuItem;
        private JMenuItem exitMenuItem;
        private JMenuItem clientsMenuItem;
        private JMenuItem acryl;
        private JMenuItem hifi;
        private JMenuItem aero;
        private JMenuItem aluminium;
        private JMenuItem bernstein;
        private JMenuItem   fast;
        private JMenuItem graphite;
        private JMenuItem luna;
        private JMenuItem mcwin;

        private JMenuItem noire;
        private JMenuItem smart;
        private JMenuBar boardMenuBar;

        private JMenuItem messengerMenuItem;
        private JMenu openMenu;
        private JMenu saveMenu;
        private JMenu themesMenu;
        private JMenuItem saveMessengerMenuItem;
        private JMenuItem saveWhiteboardMenuItem;
        private JMenu userMenu;


        JLayeredPane layerpane; 


        /** Creates new form discussionBoard 
         * @param connection */
        public DiscussionBoard() 
        {       

            initComponents();   
            setLocationRelativeTo(null);
            addComponentListener(this);

        }

        private void initComponents() {

            boardMenuBar = new JMenuBar();
            openMenu = new JMenu();
            themesMenu = new JMenu();
            messengerMenuItem = new JMenuItem();
            boardMenuItem = new JMenuItem();
            audioMenuItem = new JMenuItem();
            saveMenu = new JMenu();
            saveMessengerMenuItem = new JMenuItem();
            saveWhiteboardMenuItem = new JMenuItem();
            userMenu = new JMenu();
            clientsMenuItem = new JMenuItem();
            exitMenuItem = new JMenuItem();

            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setLayout(new GridBagLayout());
            setResizable(false);
            setTitle("Discussion Board");

            openMenu.setText("Open");
            saveMenu.setText("Save");
            themesMenu.setText("Themes");
            acryl = new JMenuItem("Acryl");
            hifi = new JMenuItem("HiFi");
            aero = new JMenuItem("Aero");
            aluminium = new JMenuItem("Aluminium");
            bernstein = new JMenuItem("Bernstein");
            fast = new JMenuItem("Fast");
            graphite = new JMenuItem("Graphite");
            luna = new JMenuItem("Luna");
            mcwin = new JMenuItem("MCwin");     
            noire = new JMenuItem("Noire");
            smart = new JMenuItem("Smart");


            hifi.addActionListener(this);
            acryl.addActionListener(this);
            aero.addActionListener(this);
            aluminium.addActionListener(this);
            bernstein.addActionListener(this);
            fast.addActionListener(this);
            graphite.addActionListener(this);
            luna.addActionListener(this);
            mcwin.addActionListener(this);      
            noire.addActionListener(this);
            smart.addActionListener(this);

            messengerMenuItem.setText("Messenger");     
            openMenu.add(messengerMenuItem);        
            openMenu.add(boardMenuItem);
            audioMenuItem.setText("Audio Messenger");
            openMenu.add(audioMenuItem);
            exitMenuItem.setText("Exit");
            exitMenuItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    exitMenuItemActionPerformed(evt);
                }
            });
            openMenu.add(exitMenuItem);

            boardMenuBar.add(openMenu);     

            saveMessengerMenuItem.setText("Messenger");

            saveMenu.add(saveMessengerMenuItem);

            saveWhiteboardMenuItem.setText("Whiteboard");       

            saveMenu.add(saveWhiteboardMenuItem);

            boardMenuBar.add(saveMenu);

            userMenu.setText("Users");
            clientsMenuItem.setText("Current Session");

            userMenu.add(clientsMenuItem);      
            themesMenu.add(acryl);
            themesMenu.add(hifi);       
            themesMenu.add(aero);
            themesMenu.add(aluminium);
            themesMenu.add(bernstein);
            themesMenu.add(fast);
            themesMenu.add(graphite);
            themesMenu.add(luna);
            themesMenu.add(mcwin);      
            themesMenu.add(noire);
            themesMenu.add(smart);
            boardMenuBar.add(userMenu);
            boardMenuBar.add(themesMenu);
            saveMessengerMenuItem.setEnabled(false);
            saveWhiteboardMenuItem.setEnabled(false);

            setJMenuBar(boardMenuBar);
            setSize(1024, 740);     
            setVisible(true);
        }

        protected void exitMenuItemActionPerformed(ActionEvent evt) {
            System.exit(0);     
        }
        @Override
        public void componentHidden(ComponentEvent arg0) {

        }

        @Override
        public void componentMoved(ComponentEvent e) {

        }

        @Override
        public void componentResized(ComponentEvent arg0) {

        }

        @Override
        public void componentShown(ComponentEvent arg0) {

        }

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            try 
            {

                if(e.getSource()==hifi)
                {
                        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                        SwingUtilities.updateComponentTreeUI(getRootPane());
                        UIManager.setLookAndFeel("com.jtattoo.plaf.hifi.HiFiLookAndFeel");
                        enableTheme();
                        hifi.setEnabled(false);
                }
                else if(e.getSource()==acryl)
                {
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");
                    enableTheme();
                    acryl.setEnabled(false);
                }
                else if(e.getSource()==aero)
                {
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.aero.AeroLookAndFeel");
                    enableTheme();
                    aero.setEnabled(false);
                }
                else if(e.getSource()==aluminium)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.aluminium.AluminiumLookAndFeel");
                    enableTheme();
                    aluminium.setEnabled(false);
                }
                else if(e.getSource()==bernstein)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
                    enableTheme();
                    bernstein.setEnabled(false);
                }
                else if(e.getSource()==fast)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.fast.FastLookAndFeel");
                    enableTheme();
                    fast.setEnabled(false);
                }
                else if(e.getSource()==graphite)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.graphite.GraphiteLookAndFeel");
                    enableTheme();
                    graphite.setEnabled(false);
                }
                else if(e.getSource()==luna)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.luna.LunaLookAndFeel");
                    enableTheme();
                    luna.setEnabled(false);
                }
                else if(e.getSource()==mcwin)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
                    enableTheme();
                    mcwin.setEnabled(false);
                }

                else if(e.getSource()==noire)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.noire.NoireLookAndFeel");
                    enableTheme();
                    noire.setEnabled(false);
                }
                else if(e.getSource()==smart)
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    SwingUtilities.updateComponentTreeUI(getRootPane());
                    UIManager.setLookAndFeel("com.jtattoo.plaf.smart.SmartLookAndFeel");
                    enableTheme();
                    smart.setEnabled(false);
                }
                SwingUtilities.updateComponentTreeUI(getRootPane());    
            }  
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
        private void enableTheme()
        {
            acryl.setEnabled(true);
            hifi.setEnabled(true);      
            aero.setEnabled(true);
            aluminium.setEnabled(true);
            bernstein.setEnabled(true);
            fast.setEnabled(true);
            graphite.setEnabled(true);
            luna.setEnabled(true);
            mcwin.setEnabled(true);     
            noire.setEnabled(true);
            smart.setEnabled(true);
        }

        public static void main(String []ar)
        {
            try 
            {
                UIManager.setLookAndFeel("com.jtattoo.plaf.acryl.AcrylLookAndFeel");            
            } 
            catch (Exception e) 
            {           
                e.printStackTrace();
            }
            new DiscussionBoard();
        }
    }

这里有什么问题?为什么它没有得到更新?

有一个演示应用程序here,它正是我想要的,但我无法清楚地了解它。

Edit: 抱歉,忘了指定库文件,here它是

3 个答案:

答案 0 :(得分:1)

在设置LnF的呼叫之后将SwingUtilities.updateComponentTreeUI(getRootPane());呼叫移至。它在传递的组件下面的树中的所有组件上调用updateUI。现在,您正在更新UI,然后更改它们。

答案 1 :(得分:0)

我不确定是什么造成了这种情况,但你看过updateUI-method吗?

来自API文档:

  

使用当前外观中的值重置UI属性。

答案 2 :(得分:0)

我尝试了你的例子,它运作良好:

alt text http://img401.imageshack.us/img401/3285/so2763157.png


我在命令行中尝试了以下文件(DiscussionBoard.java目录中的whiteboard):

JTattooDemo.jar
whiteboard/DiscussionBoard.java

javac whiteboard/DiscussionBoard.java
java -cp ".;JTattooDemo.jar" whiteboard/DiscussionBoard

请注意,JTattoo仅供免费用于非商业用途