Java 中的自定义 JMenuItem

发布于 2024-11-06 19:48:57 字数 256 浏览 4 评论 0原文

是否可以创建一个包含按钮的自定义 JMenuItem ?例如,是否可以创建一个 JMenuITem ,其项目类似于:

+----------------------------------------+
| JMenuItem [ Button | Button | Button ] |
+----------------------------------------+

Would it be possible to create a custom JMenuItem that contains buttons? For example would it be possible to create a JMenuITem with an item similar to this:

screenshot of Google Chrome's customize and control menu with the edit menu item circled

+----------------------------------------+
| JMenuItem [ Button | Button | Button ] |
+----------------------------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雅心素梦 2024-11-13 19:48:57

我怀疑是否有一种简单的方法可以做到这一点。您可以执行以下操作:

JMenuItem item = new JMenuItem("Edit                       ");
item.setLayout( new FlowLayout(FlowLayout.RIGHT, 5, 0) );
JButton copy = new JButton("Copy");
copy.setMargin(new Insets(0, 2, 0, 2) );
item.add( copy );
menu.add( item );

但存在几个问题:

a) 单击按钮时菜单不会关闭。因此,需要将该代码添加到您的 ActionListener

b) 菜单项不会响应左/右箭头等关键事件,因此无法使用键盘将焦点放在按钮上。这将涉及菜单项的 UI 更改,但我不知道从哪里开始。

我只会使用标准的 UI 设计来创建子菜单。

I doubt there is an easy way to do this. You can do something like:

JMenuItem item = new JMenuItem("Edit                       ");
item.setLayout( new FlowLayout(FlowLayout.RIGHT, 5, 0) );
JButton copy = new JButton("Copy");
copy.setMargin(new Insets(0, 2, 0, 2) );
item.add( copy );
menu.add( item );

But there are several problems:

a) the menu doesn't close when you click on the button. So that code would need to be added to your ActionListener

b) the menu item doesn't respond to key events like the left/right arrow, so there is no way to place focus on the button using the keyboard. This would involve UI changes to the menu item and I have no idea where to start for this.

I would just use the standard UI design an create sub menus.

败给现实 2024-11-13 19:48:57

我确信有,就像我个人一样,我会使用单独的菜单项,并将它们并排放置,并为每个单独的按钮设置一个动作侦听器。棘手的部分是将它们放入 JPanel 等容器中,并将它们放入流布局或网格布局中

I'm sure there is, Like personally I would use individual menuitems and just put them side by side and have an action listener for each individual button. The tricky part would be putting them inside a container like a JPanel and putting them in a flow layout or a Grid layout

客…行舟 2024-11-13 19:48:57

老问题,但你可以使用 JToolBar 相当轻松地做到这一点......

    //Make a popup menu with one menu item
    final JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem menuItem = new JMenuItem();

    //The panel contains the custom buttons
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);       
    panel.add(Box.createHorizontalGlue());        
    JToolBar toolBar = new JToolBar();
    JButton toolBarButton = new JButton();
    toolBarButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            popupMenu.setVisible(false); //hide the popup menu
            //other actions
        }
    });
    toolBar.setFloatable(false);
    toolBar.add(toolBarButton);
    panel.add(toolBar);

    //Put it all together        
    menuItem.add(panel);        
    menuItem.setPreferredSize(new Dimension(menuItem.getPreferredSize().width, panel.getPreferredSize().height)); //do this if your buttons are tall
    popupMenu.add(menuItem);

Old question, but you can do this fairly easily with a JToolBar...

    //Make a popup menu with one menu item
    final JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem menuItem = new JMenuItem();

    //The panel contains the custom buttons
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
    panel.setAlignmentX(Component.LEFT_ALIGNMENT);       
    panel.add(Box.createHorizontalGlue());        
    JToolBar toolBar = new JToolBar();
    JButton toolBarButton = new JButton();
    toolBarButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            popupMenu.setVisible(false); //hide the popup menu
            //other actions
        }
    });
    toolBar.setFloatable(false);
    toolBar.add(toolBarButton);
    panel.add(toolBar);

    //Put it all together        
    menuItem.add(panel);        
    menuItem.setPreferredSize(new Dimension(menuItem.getPreferredSize().width, panel.getPreferredSize().height)); //do this if your buttons are tall
    popupMenu.add(menuItem);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文