gpt4 book ai didi

java - 弹出菜单不显示

转载 作者:行者123 更新时间:2023-12-04 05:18:28 25 4
gpt4 key购买 nike

enter image description here

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

最佳答案

可以发一下popup的相关代码吗? .

或者更好的 SSCCE .

我无法重现您的问题。

你应该使用

saveLabel.equals(cause)

读入
cause == saveLabel

关于java - 弹出菜单不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com