JPopupMenu出现但似乎没有反应。

huangapple go评论76阅读模式
英文:

JPopupMenu appears but seems to not react

问题

我遇到了让JPopupMenu正常工作的问题。

我期望的情况

当我用鼠标右键单击时,菜单应该弹出。然后我可以从菜单中选择一个项目并执行我想做的操作。

我实际得到的情况

当我右键单击时,菜单出现了,但之后我无法选择菜单项,或者至少我错过了正常菜单中已知的鼠标悬停高亮效果(我期望当前悬停的项目会被高亮显示,就像正常菜单中的情况一样)。

在此处查看问题(没有悬停高亮):

JPopupMenu出现但似乎没有反应。

这是我的示例代码:

package com.mycompany.mavenproject2;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class PopupMenuTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        JPopupMenu popup = new JPopupMenu();
        
        popup.add(new JMenuItem("A"));
        popup.add(new JMenuItem("B"));
        
        pane.setSize(300,300);
        pane.add(popup);
        pane.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(SwingUtilities.isRightMouseButton(e)) {
                    popup.setLocation(e.getXOnScreen(), e.getYOnScreen());
                    popup.setVisible(true);
                }
            }
         });
        
        frame.setTitle("Test");
        frame.add(pane);
        frame.setPreferredSize(new Dimension(300,300));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

编辑

与正常工作的悬停效果的“正常”菜单进行比较:

JPopupMenu出现但似乎没有反应。

编辑 #2

请查看当前(意外的)行为:

JPopupMenu出现但似乎没有反应。

英文:

I've problems to get a JPopupMenu working correctly.

What I expect

The menu should pop up once I do a right click with my mouse. Then I can select an item from the menu and do whatever I want..

What I actually get

The menu appears once I do a right click but after that I can not select a menu item or at least I am missing the well known mouse hover highlight effect (I would expect that the item I am currently hovering is highlighted, like it is the case in the normal menu).

The see problem here (no highlight on hover):

JPopupMenu出现但似乎没有反应。

Here is my example code:

package com.mycompany.mavenproject2;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class PopupMenuTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel pane = new JPanel();
        JPopupMenu popup = new JPopupMenu();
        
        popup.add(new JMenuItem("A"));
        popup.add(new JMenuItem("B"));
        
        pane.setSize(300,300);
        pane.add(popup);
        pane.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(SwingUtilities.isRightMouseButton(e)) {
                    popup.setLocation(e.getXOnScreen(), e.getYOnScreen());
                    popup.setVisible(true);
                }
            }
         });
        
        frame.setTitle("Test");
        frame.add(pane);
        frame.setPreferredSize(new Dimension(300,300));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Edit

Compare to a "normal" menu with working hovering:

JPopupMenu出现但似乎没有反应。

Edit #2

Please see the current (unexpected) behaviour:

JPopupMenu出现但似乎没有反应。

答案1

得分: 1

你需要使用JPopupMenushow方法,而不是setVisible方法。

以下是我测试过的代码。我正在运行Windows 10,并使用Java JDK 13.0.2,兼容Java 8。当鼠标悬停在上面时,会出现高亮显示。

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class JPopupMenuTest implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JPopupMenuTest());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame();

        JPanel pane = new JPanel();
        pane.setPreferredSize(new Dimension(300, 300));
        pane.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    JPopupMenu popup = new JPopupMenu();
                    popup.add(new JMenuItem("A"));
                    popup.add(new JMenuItem("B"));

                    popup.show(e.getComponent(), e.getX(), e.getY());
                }
            }
        });

        frame.setTitle("JPopupMenu Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(pane);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
英文:

You need to use the JPopupMenu show method, rather than the setVisible method.

Here's the code I tested with. I'm running Windows 10 and using the Java JDK 13.0.2 with Java 8 compliance. I get the highlight on the mouse over.

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class JPopupMenuTest implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JPopupMenuTest());
}
@Override
public void run() {
JFrame frame = new JFrame();
JPanel pane = new JPanel();
pane.setPreferredSize(new Dimension(300, 300));
pane.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("A"));
popup.add(new JMenuItem("B"));
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
frame.setTitle("JPopupMenu Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

huangapple
  • 本文由 发表于 2020年8月5日 16:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63260856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定