在 jlist 上显示弹出菜单

Show popup menu on a jlist

提问人:betta7391 提问时间:7/23/2015 最后编辑:Filburtbetta7391 更新时间:7/23/2015 访问量:1502

问:

我的弹出菜单有问题。我想要的是,当用户在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,border=,flags=50331944,maximumSize=,minimumSize=,preferredSize=,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] 点击

有人可以帮助我吗?

爪哇岛

评论


答:

1赞 Sebastian 7/23/2015 #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;
    }
    //...
}

当您使用 而不是常规 JList 时,当您右键单击它时,弹出窗口将显示在正确的位置。MyJListWithPopupMenu