编程语言
首页 > 编程语言> > Java弹出按钮

Java弹出按钮

作者:互联网

注意:您可能必须编译并运行我的示例才能完全理解我的问题.如果不是犹太洁食,我事先表示歉意.

我试图创建一个基于JToggleButton和JPopupMenu的Swing控件.

如果弹出菜单可见,则选择切换按钮;如果弹出菜单不可见,则取消选择切换按钮.因此,该行为类似于JComboBox,不同之处在于弹出窗口可以包含任意组件.

下面的代码是如何创建控件的示例(除了它将在其自己的类中,例如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)如何解决这个问题,使其在所有外观上都可以使用?

解决方法:

> Nimbus过于越野车(并且开发在中间终止),与Metal相比,我需要三下鼠标单击JToggleButton
>每个标准L&F都有其特定的问题,尤其是SystemLookAndFeel
>使用JWindow而不是JPopup,因为JPopup也存在另一个Bug,例如带有JCombobox的JPopup

标签:look-and-feel,swing,nimbus,jpopupmenu,java
来源: https://codeday.me/bug/20191101/1986332.html