春風博客

          春天里,百花香...

          導航

          <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)原創,轉載請注明出處.
          主站蜘蛛池模板: 新晃| 阿拉善左旗| 威海市| 海兴县| 石河子市| 双峰县| 岳西县| 嘉黎县| 萍乡市| 大同市| 休宁县| 贵港市| 花莲市| 阿城市| 余姚市| 新乡市| 洪江市| 连平县| 儋州市| 会理县| 湖北省| 大田县| 海门市| 靖江市| 杭锦后旗| 裕民县| 新宁县| 宁城县| 从江县| 开化县| 扎囊县| 友谊县| 三都| 上蔡县| 岚皋县| 孝义市| 望江县| 樟树市| 淮阳县| 大关县| 通州区|