我的想法是创建一个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没有出现。是的,我用另一种方法设置了菜单。
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");
答案 0 :(得分:3)
答案 1 :(得分: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);
}
}