import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
public class B extends MouseAdapter {
JFrame frame = new JFrame();
JPopupMenu popup = new JPopupMenu();
JMenuItem item1 = new JMenuItem("ADD");
JMenuItem item2 = new JMenuItem("DELETE");
public static void main(String arg[]) {
new B();
}
B() {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(new FlowLayout());
frame.addMouseListener(this);
popup.add(item1);
popup.add(item2);
frame.add(popup);
frame.setVisible(true);
}
public void mouseClicked(MouseEvent click) {
if(click.isPopupTrigger())
popup.show(frame,click.getX(),click.getY());
}
}
我写的这个java代码是显示一个弹出菜单,上面有两个项目,但是当我右键单击框架时弹出菜单不显示,请帮忙。
来自 Java Doc : isPopupTrigger((
弹出菜单在不同系统上的触发方式不同。因此,应在 mousePressed 和 mouseRelease 中选中 isPopupTrigger,以获得正确的跨平台功能。
代替鼠标点击((添加以下两个方法:
public void mousePressed(MouseEvent e) {
if(e.isPopupTrigger())
popup.show(frame,e.getX(),e.getY());
}
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
popup.show(frame,e.getX(),e.getY());
}