JPopupMenu在最小化/移动JFrame时不会消失/移动



我正在制作一个mp3播放器,在我的JFrame中有几个JList。当我右键单击JList项目时,会出现一个弹出窗口,其中包含该歌曲的一些选项。但是当这个弹出窗口是可见的,并且我最小化了我的JFrame时,这个弹出窗口保持可见!此外,当弹出窗口可见,并且我将JFrame拖动到屏幕上的其他位置时,弹出窗口会停留在其原始位置(因此相对于JFrame不会停留在同一位置)。。。有人能帮我一下吗?我试着尽可能地精简这门课:)

如果有人能帮我,我将不胜感激!!

Joe

public class playListPanel extends JPanel implements MouseListener {
    private DefaultListModel model;
    private Interface interFace;
    private JList list;
    private boolean emptyPlaylist;
    private ArrayList<Song> currentPlayList;
    private Song rightClickedSong;
    private JPopupMenu popup;
    private Point panelLocation;
    public playListPanel(Interface interFace) // Interface extends JFrame,
                                                // playListPanel is a part of
                                                // this JFrame.
    {
        this.interFace = interFace;
        this.panelLocation = new Point(559, 146);
        setBackground(SystemColor.controlHighlight);
        setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        setBounds((int) panelLocation.getX(), (int) panelLocation.getY(), 698,
                368);
        setLayout(null);
        currentPlayList = new ArrayList<Song>();
        model = new DefaultListModel();
        list = new JList(model);
        list.setVisible(true);
        list.addMouseListener(this);
        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setBounds(5, 5, 688, 357);
        add(scrollPane);
        emptyPlaylist = true;
    }
    private void openMenuPopup(Point point)
    {
        removePopup();
        popup = new JPopupMenu();
        int x = (int) point.getX();
        int y = (int) point.getY();
        popup.setLocation((int) (x+panelLocation.getX()),(int) (y+panelLocation.getY()));
        //popup.setLabel("popup voor playlist");
        JMenuItem removeSong;
        popup.add(removeSong = new JMenuItem("Remove Song from Playlist", new ImageIcon("image.jpg")));
        ActionListener menuListener = new ActionListener()
        {
              public void actionPerformed(ActionEvent event) 
              {
                if(event.getActionCommand().equals("Remove Song from Playlist"))
                {
                    System.out.println("Remove Song from Playlist");
                    interFace.getPlaylistManager().removeOneSong(rightClickedSong);
                    removePopup();
                }
        };
        //ADD THE LISTENERS TO THE MENU ITEMS
        removeSong.addActionListener(menuListener);
        popup.setVisible(true);
    }
    public void removePopup()
    {
        if(popup!==null)
        {
            popup.setVisible(false);
            System.out.println("popup removed");
        }
    }
    private int getRow(Point point) {
        return list.locationToIndex(point);
    }
    public void refreshPlayList(ArrayList<Song> playlist) {
        this.currentPlayList = playlist;
        model.clear();
        for (Song song : playlist) {
            model.add(model.getSize(), song.getPlaylistString());
        }
        list.setVisible(true);
    }
    public void highlightSong(int index) {
        list.setSelectedIndex(index);
    }
    public int getRowOfList(Point point) {
        return list.locationToIndex(point);
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        interFace.getPlaylistManager().doubleClickOnPlaylist(e);
    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
    }
    @Override
    public void mouseExited(MouseEvent arg0) {
    }
    @Override
    public void mousePressed(MouseEvent e) {
        if (SwingUtilities.isRightMouseButton(e)) {
            rightClickedSong = currentPlayList.get(getRow(e.getPoint()));
            openMenuPopup(e.getPoint());
            System.out.println("should open popup at "
                    + e.getPoint().toString());
        }
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}

处理点击显示弹出窗口的方式存在一些基本缺陷。在这样的简单场景中,不建议调用popup.setVisible。相反,您可以依赖它的默认行为。此外,使用e.isPopupTrigger()比检查SwingUtilities.isRightMouseButton(e)显示弹出窗口更好。

您可以执行以下操作:

//at classlevel,
private JPopupMenu popup = new JPopupMenu();
//create a Popuplistener
PopupListener pl = new PopupListener();
list.addMouseListener(pl);
//Implementation of your popuplistener
  class PopupListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
      maybeShowPopup(e);
    }
    public void mouseReleased(MouseEvent e) {
      maybeShowPopup(e);
    }
    private void maybeShowPopup(MouseEvent e) {
      if (e.isPopupTrigger())
        //e.getSource - and construct your popup as required.
        //and then.
        popup.show(((JApplet) e.getComponent()).getContentPane(), e
            .getX(), e.getY());
    }
  }

最新更新