【问题标题】:ActionListen didn't fire on JMenuActionListen 没有在 JMenu 上触发
【发布时间】:2014-05-14 21:58:37
【问题描述】:

我是相当新的Java编程。我创建了一个JMenu 并添加了actionlisten,但它没有触发。 JPaneljpCommentMainPane 被添加到 JFrameJFrame 也有自己的菜单栏和actionlistener。有人会告诉我如何使它工作。提前致谢。

有我的代码将JMenu 添加到JPanel

jpCommentMainPane=new JPanel();         
jpCommentMainPane.setLayout(new BorderLayout(10,10));
JPanel totalCommentPane=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel jpFindPane=new JPanel(new FlowLayout(FlowLayout.LEFT));      
totalCommentPane.setPreferredSize(new Dimension(rightPaneWidth,20));        
jpFindPane.add(sortMenu());  //add menubar
JPanel topPane=new JPanel();
topPane.setLayout(new BorderLayout());
topPane.add(jpFindPane, BorderLayout.SOUTH);
jpCommentMainPane.add(topPane, BorderLayout.NORTH);

有返回JMenuBar的方法

//Create sort menu bar
private JMenuBar sortMenu(){

    JMenuBar menuBar=new JMenuBar();
    JMenu menu=new JMenu("Sort");
    JRadioButtonMenuItem menuItemType=new JRadioButtonMenuItem("Type");
    menu.add(menuItemType);
    JRadioButtonMenuItem menuItemPage=new  JRadioButtonMenuItem("Page");
    menu.add(menuItemPage);
    JRadioButtonMenuItem menuItemAuthor=new  JRadioButtonMenuItem("Author");
    menu.add(menuItemAuthor);
    JRadioButtonMenuItem menuItemDate=new  JRadioButtonMenuItem("Date");
    menu.add(menuItemDate);
    menu.addActionListener(new ActionListener(){            
    @Override
        public void actionPerformed(ActionEvent e){ 
             Utility.DisplayErrorMsg(pageErrorPrefix+ "line 145");
            String command=e.getActionCommand().trim().toUpperCase();
            if (command.equals(SortItem.TYPE)){
                pageSortBy=SortItem.TYPE;
            }else if (command.equals(SortItem.PAGE)){
                pageSortBy=SortItem.PAGE;
            }else if(command.equals(SortItem.TYPE)){
                pageSortBy=SortItem.TYPE;
            }else if(command.equals(SortItem.CREATOR)){
                pageSortBy=SortItem.CREATOR;
            }else if (command.equals(SortItem.DATE)){
                pageSortBy=SortItem.DATE;
            }

            getListCommentPane();

        }

    }

            );
    menuBar.add(menu);
    return menuBar;


}

【问题讨论】:

  • 我认为你需要将ActionListeners添加到JMenuItems,而不是JMenus。

标签: java swing jpanel jmenu


【解决方案1】:

好的,我现在确定。您可以将 ActionListener 添加到 JMenu,但与 JMenuItem 不同的是,它没有任何作用。相反,如果 JMenu 对象需要一个侦听器,您应该将一个 MenuListener 添加到您的 JMenu。

【讨论】:

  • 我将 menuListener 添加到 JMenu。还将 ActionListener 添加到 JMenuItem。但是,如果我单击 JMenuItem,它不会触发。
【解决方案2】:

JMenus 需要 MenuListeners 而不是 ActionListeners 来处理事件。 JMenuItems 将使用 ActionListeners ,但我不确定 JRadioButtonMenuItems。

要尝试的一件事是为每个 JRadioButtonMenuItem 使用内部类。

例如,对于 menuItemType

public class menuItemTypeListener implements ActionListener{
     public void ActionPerformed(ActionEvent ae){
         //Do Something here
     }
}

menuItemType.addActionListener(new menuItemTypeListener());

【讨论】:

  • JRadioButtonMenuItem 使用 ActionPerformed 和 ItemListener。谢谢你的建议。它奏效了。
猜你喜欢
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多