Chinaunix首页 | 论坛 | 博客
  • 博客访问: 145476
  • 博文数量: 54
  • 博客积分: 2682
  • 博客等级: 少校
  • 技术积分: 580
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 20:56
文章分类
文章存档

2012年(2)

2011年(10)

2010年(28)

2009年(14)

我的朋友

分类: Java

2009-12-05 20:03:35

我自己的毕业项目是用swing做的,天天看着默认的Metal风格的样式实在审美疲劳,烦死了。决定改几个外观,也好改变心情好让我继续开发。
 
记录一下代码。哈哈。
 
 

    // style manager.


    private void styleManage(JMenuBar menubar) {
        // TODO Auto-generated method stub

        JMenu styleMenu = new JMenu("Style");
        ButtonGroup bgroup = new ButtonGroup();
        String styleNow = UIManager.getLookAndFeel().getName();
        for (Iterator it = STYLE.keySet().iterator(); it.hasNext();) {
            // String styleName = STYLE.getProperty((String) it.next());

            final String radioItemTitle = (String) it.next();
            JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
                    radioItemTitle);
            styleMenu.add(radioItem);
            bgroup.add(radioItem);
            if (styleNow.equals(radioItemTitle)) {
                radioItem.setSelected(true);
            }
            radioItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub

                    // System.err.println(e.getSource());

                    String styleNow = UIManager.getLookAndFeel().getName();                    
                    TraceClientGUIJFrame mainFrame = TraceClientGUIJFrame.this;
                    if (styleNow.equals(radioItemTitle) == false) {
                        try {
                            UIManager.setLookAndFeel(mainFrame.STYLE
                                    .getProperty(radioItemTitle));
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            JOptionPane.showMessageDialog(mainFrame, ex
                                    .getClass().getName());
                            return;
                        }
                        SwingUtilities.updateComponentTreeUI(mainFrame);
                    }
                }

            });
        }
        menubar.add(styleMenu);
    }

    private static Properties STYLE = new Properties();

    static {
        // first we need add system default LookAndFeels.

        UIManager.LookAndFeelInfo[] infos = UIManager
                .getInstalledLookAndFeels();
        for (int i = 0; i < infos.length; i++) {
            STYLE.setProperty(infos[i].getName(), infos[i].getClassName());
        }
        // then we add third-party LookAndFeels. if they exist.

    };

    


 

这段代码,只记录了怎么在系统默认支持的皮肤中切换,对于怎么加入第三方的皮肤外观还不是很清楚。但愿就是添加jar包就可以了。但是可能没这么简单。以后再学。

阅读(677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~