春风博客

春天里,百花香...

导航

<2007年6月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

统计

公告

MAIL: junglesong@gmail.com
MSN: junglesong_5@hotmail.com

Locations of visitors to this page

常用链接

留言簿(11)

随笔分类(224)

随笔档案(126)

个人软件下载

我的其它博客

我的邻居们

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

一个感观(LookAndFeel)菜单类及其用法

1.类代码如下
package com.junglesong.mvc.common.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * 程序风格菜单
 * @author junglesong@gmail.com
 *
 */
public class StyleMenu extends JMenu {
  // 程序的主框架
  final JFrame mainFrame;
 
  /**
   * 构造函数
   * @param text:菜单条文字
   * @param frame:程序的主框架
   */
  public StyleMenu(String text,JFrame frame) {
    super(text);
    mainFrame=frame;
    addSubMenuItems();
  }

  /**
   * 添加下级菜单项
   *
   */
  private void addSubMenuItems() {
    // 取得系统当前可用感观数组
    UIManager.LookAndFeelInfo[] arr = UIManager
        .getInstalledLookAndFeels();

    ButtonGroup buttongroup = new ButtonGroup();
    for (int i = 0; i < arr.length; i++) {
      JRadioButtonMenuItem styleMitem = new JRadioButtonMenuItem(
          arr[i].getName(), i == 0);
      final String className = arr[i].getClassName();
     
      // 添加下级菜单项的事件相应
      styleMitem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          try {
            UIManager.setLookAndFeel(className);
            SwingUtilities.updateComponentTreeUI(mainFrame);
          } catch (Exception ex) {
            System.out.println("Can't Change Lookandfeel Style to "
                + className);
          }
        }
      });
      buttongroup.add(styleMitem);
      this.add(styleMitem);
    }
  }
}


2.用法如下

  JMenuBar menubar = new JMenuBar();
  mainFrame.setJMenuBar(menubar);
  ......
  menubar.add(Box.createHorizontalGlue());

  JMenu styleMenu = new StyleMenu("Syle", mainFrame);
  menubar.add(styleMenu);
  ......


例图:

posted on 2007-06-28 08:47 sitinspring 阅读(1889) 评论(2)  编辑  收藏 所属分类: Swing

评论

# re: 一个感观(LookAndFeel)菜单类及其用法 2007-06-29 17:42 ehe

hehe  回复  更多评论   

# re: 一个感观(LookAndFeel)菜单类及其用法[未登录] 2009-03-03 17:18 张旭

能给我把你做的(一个感观(LookAndFeel)菜单类及其用法)整个代码,发给我么,谢谢
我的邮箱是zhangxu1216@126.com  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
sitinspring(http://www.blogjava.net)原创,转载请注明出处.