2

在此处输入图像描述

这个想法是创建一个JPopupMenu当我在感叹号下方显示的LEFT CLICK
我没有使用该isPopupTrigger()方法,因为我创建了一个单独的方法MouseInputHandler来实现我的特定条件。
但是JPopupMenu没有出现。
这是代码:

protected class LabelHandler extends MouseInputAdapter{


    @Override
    public void mouseClicked(MouseEvent e){
        JLabel cause = (JLabel) e.getSource();
        if(cause == saveLabel){
            try{
                desktop.open(f);
            }catch(IOException ex){
                ex.printStackTrace();
            }
        }else if(cause == downLabel){

            popup.show(ScreenRecorder.this, e.getX(),e.getY());
        }
    }

    @Override
    public void mouseEntered(MouseEvent e){
        JLabel cause = (JLabel) e.getSource();
        if(cause == saveLabel){
            saveLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }else if(cause == downLabel){
            downLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
    }

    @Override
    public void mouseExited(MouseEvent e){
        JLabel cause = (JLabel) e.getSource();
        if(cause == saveLabel || cause == saveLabel){
            saveLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }else if(cause == downLabel){
            downLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }
}

该标签的命名是downLabel因为它应该是一个向下的箭头,但无论如何,我在那里放了一个感叹号。请告诉我为什么 JPopupMenu 没有出现。是的,我用另一种方法设置菜单。

编辑:

创建 JPopup 的代码:

public void setupJPopup(){
        encodingGroup.add(avi);
        encodingGroup.add(quicktime);

        popup.add(avi);
        popup.add(quicktime);
        popup.addSeparator();

        recordingAreaGroup.add(entireScreen);
        recordingAreaGroup.add(custom);

        popup.add(entireScreen);
        popup.add(custom);
        popup.addSeparator();

        cursorGroup.add(selectBlackCursor);
        cursorGroup.add(selectWhiteCursor);
        cursorGroup.add(selectNoCursor);

        selectCursor.add(selectBlackCursor);
        selectCursor.add(selectWhiteCursor);
        selectCursor.add(selectNoCursor);

        popup.add(selectCursor);
        popup.pack();
    }  

上述代码中涉及的变量有:

/* Creating the JPopupMenu containing the various menu items */
    JPopupMenu popup = new JPopupMenu();

    ButtonGroup encodingGroup = new ButtonGroup();
    ButtonGroup recordingAreaGroup = new ButtonGroup();
    ButtonGroup cursorGroup = new ButtonGroup();

    /* Handler for labels*/
    LabelHandler labelHandler = new LabelHandler();

    JRadioButtonMenuItem avi = new JRadioButtonMenuItem("AVI",true);
    JRadioButtonMenuItem quicktime = new JRadioButtonMenuItem("QuickTime",false);
    JRadioButtonMenuItem entireScreen = new JRadioButtonMenuItem("Entire Screen",true);
    JRadioButtonMenuItem custom = new JRadioButtonMenuItem("Custom...",false);
    JMenu selectCursor = new JMenu("Select a cursor");
    JRadioButtonMenuItem selectWhiteCursor = new JRadioButtonMenuItem("White Cursor",true);
    JRadioButtonMenuItem selectBlackCursor = new JRadioButtonMenuItem("Black Cursor",false);
    JRadioButtonMenuItem selectNoCursor = new JRadioButtonMenuItem("No Cursor",false);
/* Icons for the cursors */
    Icon blackCursor = (Icon) new ImageIcon("src/images/blackCursor.png");
    Icon whiteCursor = (Icon) new ImageIcon("src/images/whiteCursor.png");
4

2 回答 2

3

你能发布相关的代码popup

或者更好的是SSCCE

我无法重现您的问题。


你应该使用

saveLabel.equals(cause)

插入

cause == saveLabel
于 2012-12-19T07:46:19.830 回答
0

这个例子对我有用。我看到你的问题有一段时间了——然后我意识到我从来没有调用过这个setupJPopup()方法——也许这也是你的问题?否则,您是否可以开始将代码中的内容添加到此示例中,直到您重新创建问题?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PopupMenu extends Box{

    Dimension preferredSize = new Dimension(400,30);

    JPopupMenu popup = new JPopupMenu();

    ButtonGroup encodingGroup = new ButtonGroup();
    ButtonGroup recordingAreaGroup = new ButtonGroup();
    ButtonGroup cursorGroup = new ButtonGroup();

    JRadioButtonMenuItem avi = new JRadioButtonMenuItem("AVI",true);
    JRadioButtonMenuItem quicktime = new JRadioButtonMenuItem("QuickTime",false);
    JRadioButtonMenuItem entireScreen = new JRadioButtonMenuItem("Entire Screen",true);
    JRadioButtonMenuItem custom = new JRadioButtonMenuItem("Custom...",false);
    JMenu selectCursor = new JMenu("Select a cursor");
    JRadioButtonMenuItem selectWhiteCursor = new JRadioButtonMenuItem("White Cursor",true);
    JRadioButtonMenuItem selectBlackCursor = new JRadioButtonMenuItem("Black Cursor",false);
    JRadioButtonMenuItem selectNoCursor = new JRadioButtonMenuItem("No Cursor",false);
    //Icons for the cursors 
    Icon blackCursor = (Icon) new ImageIcon("src/images/blackCursor.png");
    Icon whiteCursor = (Icon) new ImageIcon("src/images/whiteCursor.png");

    public PopupMenu(){
        super(BoxLayout.Y_AXIS);

        // The part I was missing and was throwing me for a loop
        // Maybe you're not calling this method, and therefore not setting up the popup menu???
        setupJPopup();

        //Other normal stuff
        JLabel clickMe = new JLabel("ClickMe");
        clickMe.setAlignmentX(RIGHT_ALIGNMENT);
        clickMe.addMouseListener(new MouseAdapter(){

            @Override
            public void mouseClicked(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }});

        add(clickMe);       
    }

    public void setupJPopup(){
        encodingGroup.add(avi);
        encodingGroup.add(quicktime);

        popup.add(avi);
        popup.add(quicktime);
        popup.addSeparator();

        recordingAreaGroup.add(entireScreen);
        recordingAreaGroup.add(custom);

        popup.add(entireScreen);
        popup.add(custom);
        popup.addSeparator();

        cursorGroup.add(selectBlackCursor);
        cursorGroup.add(selectWhiteCursor);
        cursorGroup.add(selectNoCursor);

        selectCursor.add(selectBlackCursor);
        selectCursor.add(selectWhiteCursor);
        selectCursor.add(selectNoCursor);

        popup.add(selectCursor);
        popup.pack();
    }  


    public Dimension getPreferredSize(){
        return preferredSize;
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new PopupMenu());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }
}
于 2012-12-19T16:35:51.173 回答