这个想法是创建一个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");