java Swing的JToolBar,怎么做出这种效果?
在JToolBar上加button,在button的事件中show popupMenu,然后把menuItem加到popupMenu上,注意popupMenu与button的对齐就好了。
final JPopupMenu popupMenu = new JPopupMenu();
JMenu menu = new JMenu("menu1");
JMenuItem item1 = new JMenuItem("item1");
JMenuItem item2 = new JMenuItem("item2");
menu.add(item1);
popupMenu.add(menu);
popupMenu.add(item2);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
popupMenu.show(button, 0, button.getY() + button.getHeight());
}
});