How to create a JButton with a menu?

后端 未结 6 739
一向
一向 2020-12-30 20:16

I want to create a Toolbar in my application. If you click a button on that toolbar, it will pop up a menu, just like in Eclipse\'s toolbar. I don\'t know how to do this in

相关标签:
6条回答
  • 2020-12-30 20:19

    Here is a simple and nice class

    enter image description here

    import javax.swing.JPopupMenu;
    import javax.swing.JToggleButton;
    import javax.swing.event.PopupMenuEvent;
    import javax.swing.event.PopupMenuListener;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class MenuButton extends JToggleButton {
    
        JPopupMenu popup;
    
        public MenuButton(String name, JPopupMenu menu) {
            super(name);
            this.popup = menu;
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ev) {
                    JToggleButton b = MenuButton.this;
                    if (b.isSelected()) {
                        popup.show(b, 0, b.getBounds().height);
                    } else {
                        popup.setVisible(false);
                    }
                }
            });
            popup.addPopupMenuListener(new PopupMenuListener() {
                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    MenuButton.this.setSelected(false);
                }
                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {}
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-30 20:23

    I'm not sure I understand you correctly but if you want to know how to make toolbars in Swing check this

    Java Tutorials: How to Use Tool Bars and this

    Java Tutorials: How to Use Actions

    0 讨论(0)
  • 2020-12-30 20:24

    See the section Bringing Up a Popup Menu, in How to Use Menus.

    0 讨论(0)
  • 2020-12-30 20:29

    This is way harder in Swing than it needs to be. So instead of pointing you to tutorials I've created a fully working example.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    public class ToolbarDemo {
    
        public static void main(String[] args) {
            final JFrame frame = new JFrame();
            frame.setPreferredSize(new Dimension(600, 400));
            final JToolBar toolBar = new JToolBar();
    
            //Create the popup menu.
            final JPopupMenu popup = new JPopupMenu();
            popup.add(new JMenuItem(new AbstractAction("Option 1") {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(frame, "Option 1 selected");
                }
            }));
            popup.add(new JMenuItem(new AbstractAction("Option 2") {
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(frame, "Option 2 selected");
                }
            }));
    
            final JButton button = new JButton("Options");
            button.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            });
            toolBar.add(button);
    
            frame.getContentPane().add(toolBar, BorderLayout.NORTH);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 20:36

    I think it's the same as in AWT.

    You should put an ActionCommand on that button and when it's executed show the pop-up menu according to the mouse coordinates.

    0 讨论(0)
  • 2020-12-30 20:39

    I don't see why this is harder than it needs to be or why you should use a MouseListener. The solution by Steve McLeod works, but where the menu appears depends on where the mouse was clicked. Why not just use an ActionListener as normally used for a JButton. It seems neither harder nor less hard.

    final JPopupMenu menu = new JPopupMenu();
    menu.add(...whatever...);
    
    final JButton button = new JButton();
    button.setText("My Menu");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            menu.show(button, button.getBounds().x, button.getBounds().y
               + button.getBounds().height);
        }
    });
    

    This positions the menu about the same as a menu in a JMenuBar for me, and the position is consistent. You could place it differently by modifying the x and y in menu.show().

    0 讨论(0)
提交回复
热议问题