我的弹出菜单有问题。我想要的是,当用户在jlist上单击鼠标右键时,会出现一个弹出菜单。我创建了一个类,我创建了弹出菜单,一个扩展mouselistener的类,另一个类,我将鼠标监听器添加到jlist。 在扩展mouselistener的类中,我调用弹出菜单的类,然后显示它。 问题是弹出菜单不会出现。
package mouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import view.___poupupmenu___;
public class Add_popupmenu_categoria implements MouseListener
{
JList <String> l = new JList <String> ();
public Add_popupmenu_categoria (JList <String> l)
{
this.l = l;
}
public void mouseClicked(MouseEvent evt)
{
System.out.println("clicked");
if (evt.isPopupTrigger())
{
System.out.println("enter in clicked");
___poupupmenu___ p = new ___poupupmenu___();
l.setSelectedIndex(l.locationToIndex(evt.getPoint()));
System.out.println(evt.getComponent());
l.setComponentPopupMenu(p.menu_categoria);
p.menu_categoria.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent evt)
{
System.out.println("pressed");
if (evt.isPopupTrigger())
{
System.out.println("enter in pressed");
___poupupmenu___ p = new ___poupupmenu___();
l.setSelectedIndex(l.locationToIndex(evt.getPoint()));
System.out.println(evt.getComponent());
l.setComponentPopupMenu(p.menu_categoria);
p.menu_categoria.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
public void mouseReleased(MouseEvent evt)
{
System.out.println("released");
if (evt.isPopupTrigger())
{
System.out.println("enter in released");
___poupupmenu___ p = new ___poupupmenu___();
l.setSelectedIndex(l.locationToIndex(evt.getPoint()));
System.out.println(evt.getComponent());
l.setComponentPopupMenu(p.menu_categoria);
p.menu_categoria.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
}
package view;
import javax.swing.*;
import java.awt.*;
public class ___poupupmenu___ {
public JPopupMenu menu_categoria = new JPopupMenu();
public JPopupMenu menu_scuola = new JPopupMenu();
public JPopupMenu menu_maschile_femminile = new JPopupMenu();
public JPopupMenu menu_dirigenti_allenatori = new JPopupMenu();
public JPopupMenu menu_img_profilo = new JPopupMenu();
JMenuItem menu_elimina = new JMenuItem("Elimina");
JMenuItem menu_modifica = new JMenuItem("Modifica");
JMenuItem menu_apri = new JMenuItem("Apri...");
JMenuItem menu_pagamento = new JMenuItem("Visualizza pagamenti");
JMenuItem menu_genitore = new JMenuItem("Visualizza genitore");
JMenuItem menu_visita_medica = new JMenuItem("Visualizza scadenza visita medica");
public ___poupupmenu___ ()
{
menu_elimina.setFont(new Font("Segoe UI", 1, 15));
menu_modifica.setFont(new Font("Segoe UI", 1, 15));
menu_apri.setFont(new Font("Segoe UI", 1, 15));
menu_pagamento.setFont(new Font("Segoe UI", 1, 15));
menu_genitore.setFont(new Font("Segoe UI", 1, 15));
menu_categoria.add(menu_modifica);
menu_categoria.add(menu_elimina);
menu_scuola.add(menu_apri);
menu_scuola.add(menu_visita_medica);
menu_scuola.add(menu_genitore);
menu_scuola.add(menu_pagamento);
menu_scuola.add(menu_modifica);
menu_scuola.add(menu_pagamento);
menu_maschile_femminile.add(menu_apri);
menu_maschile_femminile.add(menu_visita_medica);
menu_maschile_femminile.add(menu_modifica);
menu_maschile_femminile.add(menu_elimina);
menu_dirigenti_allenatori.add(menu_apri);
menu_dirigenti_allenatori.add(menu_modifica);
menu_dirigenti_allenatori.add(menu_elimina);
}
}
package controller;
import javax.swing.*;
import mouseListener.Add_popupmenu_categoria;
public class CategoriaController
{
public void add_popupmenu_Categoria (JList <String> l)
{
Add_popupmenu_categoria apmc = new Add_popupmenu_categoria (l);
l.addMouseListener(apmc);
}
}
当我点击我得到的是:
压 发布 进入释放 javax.swing.JList中[,0,0,897x797,alignmentX = 0.0,alignmentY = 0.0,边界=,旗帜= 50331944,MAXIMUMSIZE =,=的minimumSize,首选大小=,fixedCellHeight,则= -1,fixedCellWidth = -1,horizontalScrollIncrement = -1 ,selectionBackground已= javax.swing.plaf.ColorUIResource [R = 184,G = 207,b = 229],selectionForeground = sun.swing.PrintColorUIResource [R = 51,G = 51,b = 51],visibleRowCount的= 8,layoutOrientation = 0] 点击
有人可以帮助我吗?
答案 0 :(得分:1)
编辑:最简单的方法是使用JList的setComponentPopupMenu
方法:
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Hello World"));
JList<String> jList = new JList<String>();
jList.setComponentPopupMenu(popup);
setComponentPopupMenu(popup)
将弹出窗口附加到组件。如果右键单击该组件,它将自动显示。不需要MouseListener。
为swing组件实现Popup-Menus的另一种简单方法是覆盖JComponent的getComponentPopupMenu()
方法。对你而言,解决方案看起来像这样:
public class MyJListWithPopupMenu extends JList {
//...
@Override
public JPopupMenu getComponentPopupMenu() {
// create your PopupMenu
return myJPopupMenu;
}
//...
}
当您使用MyJListWithPopupMenu
而不是常规JList时,弹出窗口会在您右键单击时显示在正确的位置。