下面列出了javax.swing.JMenuBar#getMenuCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* On Mac often the menus won't really update without this hack-ish twist:
* remove the menu and re-add it. Voila! It's both unnecessary and crucial
* at the same time.
*
* @param f
* the frame whose menubar you need to update.
* @param menu
* the menu you need to update.
*/
static void fixMenuBar(JFrame f, JMenu menu) {
JMenuBar mb = f.getJMenuBar();
if (mb != null) {
JMenu[] menus = new JMenu[mb.getMenuCount()];
for (int a = 0; a < menus.length; a++) {
menus[a] = mb.getMenu(a);
}
boolean found = false;
List<JMenu> menusToAdd = new ArrayList<>();
for (int a = 0; a < menus.length; a++) {
if (menus[a] == menu)
found = true;
if (found) {
mb.remove(menus[a]);
menusToAdd.add(menus[a]);
}
}
for (JMenu menuToAdd : menusToAdd) {
mb.add(menuToAdd);
}
}
}
@Override
public void addNotify() {
super.addNotify();
float size = Driver.getFontSize();
JMenuBar menubar = getJMenuBar();
if (menubar != null) {
menubar.setFont(menubar.getFont().deriveFont(size));
for (int i = 0; i < menubar.getMenuCount(); i++) {
for (int j = 0; j < menubar.getMenu(i).getMenuComponentCount(); j++) {
Component temp = menubar.getMenu(i).getMenuComponent(j);
temp.setFont(temp.getFont().deriveFont(size));
}
}
mainFrameTree.updateFonts(size);
}
}
/**
* Enables and disables the menu bars in DrawingFrames and DrawingFrame3D.
*
* Usually invoked when a model is initialized but may be invoked at other times.
*/
public static void enableMenubars(boolean enable) {
Frame[] frames = Frame.getFrames();
for(int i = 0; i<frames.length; i++) {
if(!frames[i].isDisplayable()) {
continue;
}
if((frames[i].getName()!=null)&&(frames[i].getName().indexOf("Tool")>-1)) { //$NON-NLS-1$
continue;
}
Class<?> frame3d = null;
try {
frame3d = Class.forName("org.opensourcephysics.display3d.core.DrawingFrame3D"); //$NON-NLS-1$
} catch(ClassNotFoundException ex) {}
if(DrawingFrame.class.isInstance(frames[i])||((frame3d!=null)&&frame3d.isInstance(frames[i]))) {
JMenuBar bar = ((JFrame) frames[i]).getJMenuBar();
if(bar!=null) {
for(int j = 0, n = bar.getMenuCount(); j<n; j++) {
bar.getMenu(j).setEnabled(enable);
}
}
}
}
}
/**
* Gets a menu with the given name from the menu bar. Returns null if menu item does not exist.
*
* @param menuName String
* @return JMenu
*/
public JMenu getMenu(String menuName) {
JMenuBar menuBar = getJMenuBar();
if(menuBar==null) {
return null;
}
menuName = menuName.trim();
JMenu menu = null;
for(int i = 0; i<menuBar.getMenuCount(); i++) {
JMenu next = menuBar.getMenu(i);
if(next.getText().trim().equals(menuName)) {
menu = next;
break;
}
}
return menu;
}
/**
* Removes a menu with the given name from the menu bar and returns the removed item.
* Returns null if menu item does not exist.
*
* @param menuName String
* @return JMenu
*/
public JMenu removeMenu(String menuName) {
JMenuBar menuBar = getJMenuBar();
if(menuBar==null) {
return null;
}
menuName = menuName.trim();
JMenu menu = null;
for(int i = 0; i<menuBar.getMenuCount(); i++) {
JMenu next = menuBar.getMenu(i);
if(next.getText().trim().equals(menuName)) {
menu = next;
menuBar.remove(i);
break;
}
}
return menu;
}
public void update() {
JMenuBar menuBar = menuBarMgr.getMenuBar();
if (menuBar.getMenuCount() > 0) {
node.setMenuBar(menuBar);
}
node.setToolBar(toolBarMgr.getToolBar());
node.validate();
}
public static void setMnemonics(final JMenuBar menuBar) {
final int c = menuBar.getMenuCount();
final ArrayList<JMenuItem> list = new ArrayList<JMenuItem>(c);
for (int i = 0; i < c; i++)
list.add(menuBar.getMenu(i));
setMnemonics(list);
}
public static int findMenuPosition(JMenuBar menuBar, String name) {
int n = menuBar.getMenuCount();
for (int i = 0; i < n; i++) {
JMenu menu = menuBar.getMenu(i);
if (name.equals(menu.getName())) {
return i;
}
}
return -1;
}