如何创建一个“下拉” Java Swing工具栏中的菜单? [英] How can I create a "Drop-Down" menu in a Java Swing toolbar?

查看:283
本文介绍了如何创建一个“下拉” Java Swing工具栏中的菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Swing JToolBar上创建了一个下拉菜单。但是它不会创造出我想要的方式。我的目标是像Firefox的智能书签按钮一样工作。



当用户选择菜单项时,它消失:CORRECT!



当用户按ESC ESC:CORRECT!



当用户单击菜单外的主框架某处时,它将消失:CORRECT !



但是,当用户在显示下拉菜单的按钮上再次单击时不会消失:INCORRECT ...: - (



我的问题是我如何添加这个行为,当它第二次显示菜单的按钮的点击时它会消失。



这是我当前的代码,从Java 6在Mac上:

  import javax.swing。*; 
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt。*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ScratchSpace {

public stati c void main(String [] arguments){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame frame = new JFrame(Toolbar with Popup Menu demo );

final JToolBar toolBar = new JToolBar();
toolBar.add(createMoreButton());

final JPanel panel = new JPanel(new BorderLayout());
panel.add(toolBar,BorderLayout.NORTH);
panel.setPreferredSize(new Dimension(600,400));
frame.getContentPane()。add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

private static AbstractButton createMoreButton(){
final JToggleButton moreButton = new JToggleButton(More ...);
moreButton.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()== ItemEvent.SELECTED){
createAndShowMenu(( JComponent)e.getSource(),moreButton);
}
}
});
moreButton.setFocusable(false);
moreButton.setHorizo​​ntalTextPosition(SwingConstants.LEADING);
return moreButton;
}

private static void createAndShowMenu(final JComponent component,final AbstractButton moreButton){
JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem(Black));
menu.add(new JMenuItem(Red));

menu.addPopupMenuListener(new PopupMenuListener(){
public void popupMenuWillBecomeVisible(PopupMenuEvent e){
}

public void popupMenuWillBecomeInvisible(PopupMenuEvent e){
moreButton.setSelected(false);
}

public void popupMenuCanceled(PopupMenuEvent e){
moreButton.setSelected(false);
}
});

menu.show(component,0,component.getHeight());
}
}


解决方案

,这是一个潜在的解决方案,而不是没有它的缺点。只有您可以决定是否可以接受您的申请。问题是弹出关闭发生在其他鼠标处理事件被触发之前,再次点击更多..按钮会导致弹出窗口隐藏,从而在按钮甚至被按下之前重置按钮状态以取消选择。 p>

容易的解决方法是在主程序中添加以下调用:

  UIManager.put(PopupMenu.consumeEventOnClose,Boolean.TRUE); 

这样做的结果是,每当弹出菜单因为鼠标按下事件而关闭时,鼠标事件将在菜单关闭时消耗,不会传递到鼠标下方的任何其他组件。如果你能够受限制,这是一个简单的解决方案。


I've created a drop-down menu on my Swing JToolBar. But it doesn't create behave the way I want. I'm aiming for it to work like Firefox's "Smart Bookmarks" button.

It disappears when the user selects a menu item: CORRECT!

It disappears when the user presses ESC: CORRECT!

It disappears when the user clicks somewhere in the main frame outside of the menu: CORRECT!

But it doesn't disappear when the user clicks a second time on the button that shows the drop-down menu: INCORRECT... :-(

My question is how can I add this behaviour, that it does disappear when the clicks on the button that shows the menu a second time.

Here's my current code, from Java 6 on the Mac:

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ScratchSpace {

    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Toolbar with Popup Menu demo");

                final JToolBar toolBar = new JToolBar();
                toolBar.add(createMoreButton());

                final JPanel panel = new JPanel(new BorderLayout());
                panel.add(toolBar, BorderLayout.NORTH);
                panel.setPreferredSize(new Dimension(600, 400));
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    private static AbstractButton createMoreButton() {
        final JToggleButton moreButton = new JToggleButton("More...");
        moreButton.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    createAndShowMenu((JComponent) e.getSource(), moreButton);
                }
            }
        });
        moreButton.setFocusable(false);
        moreButton.setHorizontalTextPosition(SwingConstants.LEADING);
        return moreButton;
    }

    private static void createAndShowMenu(final JComponent component, final AbstractButton moreButton) {
        JPopupMenu menu = new JPopupMenu();
        menu.add(new JMenuItem("Black"));
        menu.add(new JMenuItem("Red"));

        menu.addPopupMenuListener(new PopupMenuListener() {
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                moreButton.setSelected(false);
            }

            public void popupMenuCanceled(PopupMenuEvent e) {
                moreButton.setSelected(false);
            }
        });

        menu.show(component, 0, component.getHeight());
    }
}

解决方案

Well, here is a potential solution that is not without it's drawbacks. Only you can decide if this is acceptable for your application. The issue is that the popup closing occurs before other mouse-handling events are fired so clicking on your More.. button again causes the popup to hide, thus resetting the buttons state to deselected BEFORE the button even gets told it was pressed.

The easy workaround is to add the following call within your main program:

UIManager.put("PopupMenu.consumeEventOnClose", Boolean.TRUE);

The result of this is that whenever a popup menu is closed because of a mouse-pressed event, that mouse event will be consumed at the time the menu is closed and won't be passed on to any other components under the mouse. If you can live with limitation, this is an easy solution.

这篇关于如何创建一个“下拉” Java Swing工具栏中的菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆