我的JFrame
包含JTabbedPane
,其中包含JPanel
。
在此JPanel
中,我希望在单击鼠标右键时在鼠标位置显示JPopupMenu
。
为此,我使用show(invoker, x, y)
方法。
我的问题:JPopupMenu
有一种非常奇怪的行为;有时它显示时不包含所有内容(只是一个灰色框),有时显示在Panel
的左上角,表现完全符合预期。
代码:
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
class Testframe extends JFrame {
public static JFrame frame;
private static final long serialVersionUID = 1L;
public Testframe(String string) {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle(string);
setSize(200,200);
setVisible(true);
}
public static void main(String[] args) {
frame = new Testframe("Title");
JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
tabpane.addTab("title", new TestPanel());
frame.add(tabpane);
tabpane.setVisible(true);
}
}
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class TestPanel extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
JPopupMenu activeDropdown;
TestPanel() {
setBackground(Color.GREEN);
setVisible(true);
addMouseListener(this);
}
private void dropdown(MouseEvent e) {
activeDropdown = new JPopupMenu();
JMenuItem item = new JMenuItem("Eintrag 0");
activeDropdown.add(item);
activeDropdown.show(Testframe.frame, e.getX(), e.getY());
this.add(activeDropdown);
}
@Override
public void mouseClicked(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
if (activeDropdown != null)
this.remove(activeDropdown);
dropdown(e);
}
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
}
如果我尝试将JTabbedPane
放入单独的类中,JPopupMenu
会出现在屏幕上的任何位置(似乎是固定位置),完全独立于窗口位置。
答案 0 :(得分:1)
更改您的dropdown
方法,如下所示。这应该按预期工作。
private void dropdown(MouseEvent e) {
activeDropdown = new JPopupMenu();
JMenuItem item = new JMenuItem("Eintrag 0");
activeDropdown.add(item);
this.add(activeDropdown);
activeDropdown.show(this, e.getX(), e.getY());
}
但是,我不明白你为什么要删除现有的JPopMenu
并在每次点击鼠标时添加一个新的。{/ p>
答案 1 :(得分:1)
您只需使用JComponent.setComponentPopupMenu
即可处理直接点击弹出式菜单。这样更简单,并且可以为您处理所有接线代码。
代码的小例子:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class TestPanel extends JPanel {
private static final long serialVersionUID = 1L;
JPopupMenu activeDropdown;
TestPanel() {
setBackground(Color.GREEN);
activeDropdown = new JPopupMenu();
JMenuItem item = new JMenuItem("Eintrag 0");
activeDropdown.add(item);
setComponentPopupMenu(activeDropdown);
}
protected void initUI() {
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabpane = new JTabbedPane(JTabbedPane.TOP);
tabpane.addTab("title", this);
frame.add(tabpane);
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestPanel().initUI();
}
});
}
}
注意:避免使用static
变量!