我先开个头吧
程序代码:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Test {
private static final String MENU_MODI_PASS = "密码修改";
private static final String MENU_RELOGIN = "重新登陆";
private static final String MENU_LOGOUT = "退出系统";
private static final String MENU_ADD = "录入";
private static final String MENU_QUERY = "查找";
private static final String MENU_UPDATE = "更改";
private static final String MENU_DELETE = "删除";
private static final String MENU_CAL = "计算器";
private static final String MENU_BACK_CHK = "背景更改";
private static final String MENU_NOTE = "记事本";
private static final String MENU_HELP = "帮助";
private static final String MENU_ABOUT = "关于";
private static final int DEFAULT_WIDTH = 460;
private static final int DEFAULT_HIEGHT = 640;
public static void main(String[] args) {
JFrame jframe = new JFrame();
Map<String, List<String>> map = new LinkedHashMap<>();
map.put("系统", Arrays.asList(MENU_MODI_PASS, MENU_RELOGIN, MENU_LOGOUT));
map.put("管理", Arrays.asList(MENU_ADD, MENU_QUERY, MENU_UPDATE, MENU_DELETE));
map.put("工具", Arrays.asList(MENU_CAL, MENU_BACK_CHK, MENU_NOTE));
map.put("帮助", Arrays.asList(MENU_HELP, MENU_ABOUT));
JMenuBar jMenuBar = new JMenuBar();
jframe.setJMenuBar(jMenuBar);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JMenu menu = (JMenu)e.getSource();
System.out.println(menu.getText());
}
};
map.forEach((k,v)->{
JMenu menu = new JMenu(k);
jMenuBar.add(menu);
v.forEach(val->{
JMenu menuCell = new JMenu(val);
menuCell.addMouseListener(mouseAdapter);
menu.add(menuCell);
});
});
jframe.setSize(DEFAULT_WIDTH, DEFAULT_HIEGHT);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.setTitle("车辆档案管理系统");
}
}