阻止JPopupMenu被任务栏

时间:2018-06-08 15:01:33

标签: java swing jpopupmenu

如何让JPopupMenu出现在任务栏上方?换句话说,我如何强制它遵守屏幕限制,以免它被覆盖?以Android Studio的弹出菜单为例:

正常位置:

enter image description here

当我将窗口拖到底部任务栏附近时,弹出“适应”并显示在其上方:

现在我的测试用例: 正常位置:

在任务栏附近(您可以看到,与Android Studio不同,弹出窗口的一部分会在任务栏下消失):

测试用例代码:

Test.java

public class Test extends javax.swing.JFrame {

    public Test() {
        initComponents();
    }

    public void initUI() {

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        btnMenu = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnMenu.setText("Click for Menu");
        btnMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(291, Short.MAX_VALUE)
                .addComponent(btnMenu)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(266, Short.MAX_VALUE)
                .addComponent(btnMenu)
                .addContainerGap())
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void btnMenuActionPerformed(java.awt.event.ActionEvent evt) {                                        
        JPopupMenu menu = new JPopupMenu();
        menu.add(new PopBody());
        menu.show(this, btnMenu.getLocation().x - 95, btnMenu.getLocation().y + 60);
    }                                       

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        }
        java.awt.EventQueue.invokeLater(() -> new Test().setVisible(true));
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnMenu;
    // End of variables declaration                   
}

PopBody.java

public class PopBody extends javax.swing.JPanel {

    public PopBody() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        lblBody = new javax.swing.JLabel();
        btnOK = new javax.swing.JButton();

        lblBody.setText("Panel body");

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(77, 77, 77)
                .addComponent(lblBody)
                .addContainerGap(92, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnOK)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(lblBody)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(btnOK)
                .addContainerGap())
        );
    }// </editor-fold>                        

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {                                      

    }                                     


    // Variables declaration - do not modify                     
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel lblBody;
    // End of variables declaration                   
}

1 个答案:

答案 0 :(得分:0)

我自己偶然发现了这个问题,并提出了解决方案。

private void btnMenuActionPerformed(ActionEvent evt) {
    JPopupMenu popup = new JPopupMenu();
    popup.add(new PopBody());
    popup.pack();

    int popUpHeight = popup.getPreferredSize().height;
    //determines the max available size of the screen
    Rectangle windowBounds = GraphicsEnvironment.
        getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Point invokerPosition = ((Component)evt.getSource()).getLocationOnScreen();

    if (invokerPosition.y + popUpHeight > windowBounds.height) {
        //get the negative margin and use it as the y-offset
        popup.show(this, 0, windowBounds.height - (invokerPosition.y + popUpHeight));
    }
    else {
        popup.show(this, 0, 0);
    }
}

我不确定您的硬编码偏移量。