春風博客

          春天里,百花香...

          導航

          <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 閱讀(1923) 評論(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.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 即墨市| 邹城市| 乌海市| 庐江县| 敦化市| 双鸭山市| 丰镇市| 岑巩县| 肥乡县| 招远市| 涿州市| 富裕县| 吉林省| 龙岩市| 中牟县| 福州市| 莆田市| 馆陶县| 彩票| 白朗县| 湄潭县| 庐江县| 霸州市| 洛扎县| 隆安县| 陵川县| 白银市| 马关县| 汤原县| 称多县| 绍兴市| 陕西省| 丹棱县| 长岭县| 紫金县| 礼泉县| 阿拉善盟| 云霄县| 远安县| 中江县| 广宁县|