Swing - 删除JMenu中除最后一个JMenuItem之外的所有内容

时间:2014-08-11 22:02:24

标签: java swing

我试图删除每个JMenuItem但是当点击一个按钮时从JMenu中移除最后一个JMenuItem但是虽然我让它在前几次点击时工作,但它会在连续几次尝试后停止工作。

    int count = ojmenu.getMenuComponentCount();
    for(int i = 0; i<count-1 ; i++)
    {
        ojmenu.remove(i);
    }

2 个答案:

答案 0 :(得分:1)

您是否考虑过删除所有菜单项并重新添加最后一个菜单项,例如......

int count = menu.getMenuComponentCount();
Component last = menu.getMenuComponent(count - 1);
menu.removeAll();
menu.add(last);

您应该注意JMenu可能包含其他组件,因此使用remove(int)实际上可能不会删除菜单项,而是另一个组件......

Menus

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
import org.jdesktop.jxlayer.JXLayer;
import org.pbjar.jxlayer.demo.TransformUtils;
import org.pbjar.jxlayer.plaf.ext.transform.DefaultTransformModel;

public class MenuExample {

    public static void main(String[] args) {
        new MenuExample();
    }

    public MenuExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JMenu menu = new JMenu("Test");

                JButton build = new JButton("Build");
                build.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        makeItSo(menu);
                    }
                });
                JButton remove = new JButton("Remove");
                remove.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int count = menu.getMenuComponentCount();
                        Component last = menu.getMenuComponent(count - 1);
                        menu.removeAll();
                        menu.add(last);
                    }
                });

                makeItSo(menu);

                JPanel buttons = new JPanel();
                buttons.add(build);
                buttons.add(remove);

                JMenuBar mb = new JMenuBar();
                mb.add(menu);

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setJMenuBar(mb);
                frame.add(buttons, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected void makeItSo(JMenu menu) {
        for (int index = 0; index < (Math.random() * 20); index++) {
            menu.add("MenuItem " + index);
        }
    }
}

答案 1 :(得分:0)

尝试向后循环(从倒数第二个项开始)而不是向前循环:

int count = ojmenu.getMenuComponentCount();
for (int i = count - 2; i >= 0; i--)
{
    ojmenu.remove(i);
}