春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

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

          統(tǒng)計

          公告

          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;

          /**
           * 程序風(fēng)格菜單
           * @author junglesong@gmail.com
           *
           */
          public class StyleMenu extends JMenu {
            // 程序的主框架
            final JFrame mainFrame;
           
            /**
             * 構(gòu)造函數(shù)
             * @param text:菜單條文字
             * @param frame:程序的主框架
             */
            public StyleMenu(String text,JFrame frame) {
              super(text);
              mainFrame=frame;
              addSubMenuItems();
            }

            /**
             * 添加下級菜單項
             *
             */
            private void addSubMenuItems() {
              // 取得系統(tǒng)當(dāng)前可用感觀數(shù)組
              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();
               
                // 添加下級菜單項的事件相應(yīng)
                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 閱讀(1917) 評論(2)  編輯  收藏 所屬分類: Swing

          評論

          # re: 一個感觀(LookAndFeel)菜單類及其用法 2007-06-29 17:42 ehe

          hehe  回復(fù)  更多評論   

          # re: 一個感觀(LookAndFeel)菜單類及其用法[未登錄] 2009-03-03 17:18 張旭

          能給我把你做的(一個感觀(LookAndFeel)菜單類及其用法)整個代碼,發(fā)給我么,謝謝
          我的郵箱是zhangxu1216@126.com  回復(fù)  更多評論   

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請注明出處.
          主站蜘蛛池模板: 保靖县| 唐山市| 哈尔滨市| 高尔夫| 嘉义县| 博乐市| 瑞金市| 湘阴县| 拉萨市| 汤原县| 宜春市| 常宁市| 华宁县| 瑞丽市| 泽普县| 密云县| 阿城市| 丹阳市| 无锡市| 旅游| 富民县| 南雄市| 厦门市| 讷河市| 太白县| 增城市| 永清县| 图木舒克市| 额济纳旗| 迁西县| 青州市| 丰原市| 丰县| 壤塘县| 道真| 安徽省| 兰考县| 渭源县| 房产| 诏安县| 桓台县|