3

注意:您可能必须编译并运行我的示例才能完全理解我的问题。如果这不是犹太洁食,我提前道歉。

我正在尝试创建一个基于 aJToggleButton和 a的 Swing 控件JPopupMenu

如果弹出菜单可见,则选择切换按钮,如果弹出菜单不可见,则取消选择切换按钮。因此,行为类似于 a JComboBox,只是弹出窗口可以包含任意组件。

下面的代码是我将如何创建控件的示例(除了它将在自己的类中......类似于 a JPopupToggleButton)。不幸的是,它在不同的外观和感觉下表现出不同的行为(我已经用 Metal 和 Nimbus 对其进行了测试)。

此处发布的代码在 Metal 中的行为符合预期,但在 Nimbus 中则不然。使用 Nimbus 时,只需通过反复单击切换按钮来显示和隐藏弹出窗口,您就会明白我的意思。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class PopupButtonExample extends JFrame
{
    public static void main( String[] args )
    {
        java.awt.EventQueue.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                PopupButtonExample example = new PopupButtonExample();
                example.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                example.setVisible( true );
            }
        });
    }

    public PopupButtonExample()
    {
        super( "Components in Popup" );

        JPanel popupPanel = new JPanel();
        popupPanel.setLayout( new BorderLayout() );
        popupPanel.add( new JLabel( "This popup has components" ),
                BorderLayout.NORTH );
        popupPanel.add( new JTextArea( "Some text", 15, 20 ),
                BorderLayout.CENTER );
        popupPanel.add( new JSlider(), BorderLayout.SOUTH );

        final JPopupMenu popupMenu = new JPopupMenu();
        popupMenu.add( popupPanel );

        final JToggleButton popupButton = new JToggleButton( "Show Popup" );
        popupButton.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if( popupButton.isSelected() )
                    popupMenu.show( popupButton, 0, popupButton.getHeight() );
            }
        });

        popupMenu.addPopupMenuListener( new PopupMenuListener()
        {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent pme) {}

            @Override
            public void popupMenuCanceled(PopupMenuEvent pme) {}

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent pme) {
                Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
                Point componentLoc = popupButton.getLocationOnScreen();
                mouseLoc.x -= componentLoc.x;
                mouseLoc.y -= componentLoc.y;
                if( !popupButton.contains( mouseLoc ) )
                    popupButton.setSelected( false );
            }
        });

        JPanel toolBarPanel = new JPanel();
        toolBarPanel.add( popupButton );
        JToolBar toolBar = new JToolBar();
        toolBar.add( toolBarPanel );

        setLayout( new BorderLayout() );
        add( toolBar, BorderLayout.PAGE_START );
        setPreferredSize( new Dimension( 640, 480 ) );
        pack();
    }
}

排除以下行使代码在 Nimbus 中的行为符合预期,但在 Metal 中则不然。同样,只需继续单击切换按钮即可了解我的意思。

//                Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
//                Point componentLoc = popupButton.getLocationOnScreen();
//                mouseLoc.x -= componentLoc.x;
//                mouseLoc.y -= componentLoc.y;
//                if( !popupButton.contains( mouseLoc ) )

所以这是我的两个问题:

(1) 在 Nimbus 中,为什么隐藏弹出面板的点击没有传递给切换按钮,就像使用 Metal 一样?

(2) 我怎样才能解决这个问题,使它适用于所有的外观和感觉?

4

2 回答 2

4
  • Nimbus 太麻烦了(并且开发在中间的某个地方结束了)我看到与 Metal 相比,您需要在 JToggleButton 上单击三下鼠标

  • 每个标准 L&F 都有自己的特定问题,尤其是 SystemLookAndFeel

  • 使用 JWindow 而不是 JPopup,因为使用 JPopup 还有另一个错误,例如带有 JCombobox 的 JPopup

于 2012-01-18T21:59:10.557 回答
0

经过一番调查,我找到了 Nimbus 和 Metal 之间差异的原因。以下标志用于(至少由BasicPopupMenuUI)在弹出窗口关闭时控制事件的消耗:

UIManager.getBoolean( "PopupMenu.consumeEventOnClose" );

使用 Nimbus 时,返回true. 使用 Metal 时,返回false. 因此,该方法popupMenuWillBecomeInvisible应定义如下:

if( UIManager.getBoolean( "PopupMenu.consumeEventOnClose" ) )
{
    popupButton.setSelected( false );
}
else
{
    Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
    Point componentLoc = popupButton.getLocationOnScreen();
    mouseLoc.x -= componentLoc.x;
    mouseLoc.y -= componentLoc.y;
    if( !popupButton.contains( mouseLoc ) )
    {
        popupButton.setSelected( false );
    }
}
于 2012-01-19T18:21:01.793 回答