2

JFrameにいくつかのJListを入れて、mp3プレーヤーを作成しています。JListアイテムを右クリックすると、その曲のいくつかのオプションを含むポップアップが表示されます。しかし、このポップアップが表示されていて、JFrameを最小化すると、このポップアップは表示されたままになります。また、ポップアップが表示されていて、JFrameを画面上の別の場所にドラッグすると、ポップアップは元の位置に留まります(したがって、JFrameに対して同じ位置に留まりません)...誰か助けてくださいこれで出ますか?私はこのクラスを可能な限り取り除こうとしました:)

誰かが私を助けてくれたら本当にありがたいです!!

ジョー

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) {
    }
}
4

1 に答える 1

1

ポップアップを表示するためのクリックの処理方法には、いくつかの基本的な欠陥があります。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());
    }
  }
于 2012-01-04T22:35:57.087 に答える