JAVA:简单添加菜单界面(swing)第二版

环境:jdk1.8

  1 package com.le.tool;
  2 
  3 import java.awt.Color;
  4 import java.awt.Container;
  5 import java.awt.FlowLayout;
  6 import java.awt.GridLayout;
  7 import java.awt.event.ActionEvent;
  8 import java.awt.event.ActionListener;
  9 import java.awt.event.ItemEvent;
 10 import java.awt.event.ItemListener;
 11 import java.awt.event.KeyEvent;
 12 import java.util.ArrayList;
 13 import java.util.HashMap;
 14 import java.util.List;
 15 import java.util.Map;
 16 import java.util.Map.Entry;
 17 
 18 import javax.swing.JFrame;
 19 import javax.swing.JLabel;
 20 import javax.swing.JMenu;
 21 import javax.swing.JMenuBar;
 22 import javax.swing.JMenuItem;
 23 import javax.swing.JPanel;
 24 import javax.swing.JRadioButtonMenuItem;
 25 
 26 import com.le.entity.MyMenu;
 27 
 28 public class MenuManagerUtils {
 29     
 30     
 31     /**
 32      * 菜单map:<语言种类,List<菜单信息>>
 33      */
 34     private final static Map<String, List<MyMenu>> menuMap;
 35     static
 36     {
 37         List<MyMenu> orginMenu = getOrginMenuMap();
 38         menuMap = new HashMap<String, List<MyMenu>>();
 39         for (int i = 0; i < orginMenu.size(); i++) {
 40             MyMenu myMenu = orginMenu.get(i);
 41             
 42             if (null == menuMap.get(myMenu.getCodeLanguage()))
 43             {
 44                 List<MyMenu> menuTemp = new ArrayList<>();
 45                 menuTemp.add(myMenu);
 46                 menuMap.put(myMenu.getCodeLanguage(), menuTemp);
 47             }
 48             else
 49             {
 50                 menuMap.get(myMenu.getCodeLanguage()).add(myMenu);
 51             }
 52         }
 53     }
 54     
 55     /**
 56      * 初始化菜单数据
 57      * 
 58      * @return List<MyMenu>
 59      */
 60     private static List<MyMenu> getOrginMenuMap()
 61     {
 62         List<MyMenu> menuList = new ArrayList<MyMenu>();
 63         // 此处赋初始化的数据,假数据测试
 64         for (int i = 0; i < 5; i++) {
 65             int index = i + 1;
 66             MyMenu menu = null;
 67             if (i%2==0)
 68             {
 69                 menu = new MyMenu("testFunctionName" + index, "Java" + index, "testClass" + index, "testMethod" + index,
 70                         "description" + index);
 71             }
 72             else
 73             {
 74                 menu = new MyMenu("testFunctionName" + index, "C" + index, "testClass" + index, "testMethod" + index,
 75                         "description" + index);
 76             }
 77             menuList.add(menu);
 78         }
 79         return menuList;
 80     }
 81     
 82     public MenuManagerUtils() {
 83         init();
 84     }
 85 
 86     public static void main(String[] args) {
 87         MenuManagerUtils menuManager = new MenuManagerUtils();
 88         menuManager.show();
 89     }
 90 
 91     public void show() {
 92         for (Entry<String, List<MyMenu>> entry : menuMap.entrySet()) {
 93             System.out.println("key= " + entry.getKey() + " and value= "
 94                     + entry.getValue());
 95             List<MyMenu> menuList = entry.getValue();
 96             initMenuBar(mainFrame, menuList);
 97         }
 98 
 99         mainFrame.setVisible(true);
100     }
101 
102     /**
103      * 
104      * 这里处理对应的ActionCommand(ActionEvent)的处理
105      *
106      */
107     class MenuItemListener implements ActionListener {
108         @Override
109         public void actionPerformed(ActionEvent e) {
110             statusLabel.setText("JMenu Item clicked:" + e.getActionCommand() + ".");
111         }
112     }
113 
114     /**
115      * 给主窗口设置背景色
116      * 
117      * @param mainFrame
118      *            主窗口
119      * @see 分析:JFrame默认使用流式布局管理器(FlowLayout)将整个窗体进行覆盖操作,<br>
120      *      设置的颜色被布局管理器给覆盖住了,所以无法看见。<br>
121      *      解决:在JFrame中添加一个面板容器,使得面板容器对窗体覆盖即可。<br>
122      */
123     private void initFrameBgColor(JFrame mFrame) {
124         Container pane = mFrame.getContentPane();
125         pane.setBackground(Color.orange);
126     }
127 
128     /**
129      * 标题
130      */
131     private static final String MENU_MANAGER_TITILE = "Manager Tool";
132 
133     private JFrame mainFrame;
134     private JLabel headerLabel;
135     private JLabel statusLabel;
136     private JPanel controlPanel;
137 
138     private void init() {
139         // 主界面
140         mainFrame = new JFrame();
141         // 标题
142         mainFrame.setTitle(MENU_MANAGER_TITILE);
143         // 设置窗口大小
144         mainFrame.setSize(400, 400);
145         // 设置窗体的位置属性
146         mainFrame.setLocation(400, 200);
147         // 设置窗体关闭时退出程序(3)
148         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
149         /*
150          * mainFrame.addWindowListener(new WindowAdapter() { public void
151          * windowClosing(WindowEvent windowEvent) { System.exit(0); } });
152          */
153         // 设置允许调整窗体大小
154         mainFrame.setResizable(true);
155         mainFrame.setLayout(new GridLayout(3, 1));
156         // 下边这种方式设置第一个Frame的背景色不生效,需要用initFrameBgColor()里的方式
157         // mainFrame.setBackground(Color.blue);
158         initFrameBgColor(mainFrame);
159 
160         headerLabel = new JLabel("11", JLabel.CENTER);
161         statusLabel = new JLabel("22", JLabel.CENTER);
162         controlPanel = new JPanel();
163         controlPanel.setLayout(new FlowLayout());
164         controlPanel.setBackground(Color.gray);
165 
166         mainFrame.add(headerLabel);
167         mainFrame.add(controlPanel);
168         mainFrame.add(statusLabel);
169     }
170 
171     /**
172      * 初始化菜单栏
173      * 
174      * @param mFrame
175      * @param myMenu
176      */
177     private void initMenuBar(JFrame mFrame, List<MyMenu> menuList) {
178         if (menuList.isEmpty()) {
179             return;
180         }
181         JMenuBar menuBar = mFrame.getJMenuBar();
182         if (null == menuBar)
183         {
184             // 创建菜单栏
185             menuBar = new JMenuBar();
186         }
187         // 创建菜单栏菜单
188         JMenu jMenu = new JMenu(menuList.get(0).getCodeLanguage());
189         for (int i = 0; i < menuList.size(); i++) {
190             MyMenu myMenu = menuList.get(i);
191             // 创建菜单栏菜单
192             // JMenu jMenu = new JMenu("功能列表" + (i + 1));
193             initJMenuItem(jMenu, myMenu);
194             
195             initJRadioButtonItem(jMenu, myMenu);
196             
197             menuBar.add(jMenu);
198         }
199         
200         // 添加菜单栏后,下边的组件高度会被挤压变小
201         mFrame.setJMenuBar(menuBar);
202     }
203 
204     /**
205      * 初始化菜单栏子项:JMenu
206      * 
207      * @param menuBar
208      * @param myMenu
209      */
210     private void initJMenuItem(JMenu jMenu, MyMenu myMenu) {
211         // 创建菜单栏菜单子项
212         JMenuItem newMenuItem = new JMenuItem(myMenu.getName());
213         newMenuItem.setMnemonic(KeyEvent.VK_N);
214         newMenuItem.setActionCommand(myMenu.getName());
215         // 创建监听器
216         MenuItemListener menuItemListener = new MenuItemListener();
217         // 添加点击的动作
218         newMenuItem.addActionListener(menuItemListener);
219         jMenu.add(newMenuItem);
220     }
221     
222     /**
223      * 初始化菜单栏子项:JMenu
224      * 
225      * @param menuBar
226      * @param myMenu
227      */
228     private void initJRadioButtonItem(JMenu jMenu, MyMenu myMenu) {
229         // 创建菜单栏菜单子项:允许是否选中
230         final JRadioButtonMenuItem showLinksMenu = new JRadioButtonMenuItem("Show Links", true);
231         showLinksMenu.addItemListener(new ItemListener() {
232             @SuppressWarnings("static-access")
233             public void itemStateChanged(ItemEvent e) {
234                 // state,结果1表示选中,结果2表示不选中
235                 System.out.println(e.getStateChange());
236                 if (e.SELECTED == e.getStateChange())
237                 {
238                     showLinksMenu.setText("Show Link:Enable");
239                     mainFrame.repaint();
240                 }
241                 else
242                 {
243                     showLinksMenu.setText("Show Link:Disable");
244                     mainFrame.repaint();
245                 }
246                 // 创建监听器
247                 MenuItemListener menuItemListener = new MenuItemListener();
248                 // 添加点击的动作
249                 showLinksMenu.addActionListener(menuItemListener);
250             }
251         });
252         jMenu.add(showLinksMenu);
253     }
254 }

猜你喜欢

转载自www.cnblogs.com/leonlipfsj/p/10351629.html