实现右击控件,弹出菜单
效果:
假如要右击图片ImageView,弹出菜单项,实现步骤:
1.给控件设置右击弹出菜单事件
//PerspectiveImage extends Parent
PerspectiveImage item=new PerspectiveImage ();
item.setOnContextMenuRequested(
event -> contextMenu.show(this, event.getScreenX(), event.getScreenY())
);
contextMenu
下一步创建
2.创建菜单 Context Menu
ContextMenu contextMenu = new ContextMenu();
MenuItem frameBegin = new MenuItem("设为开始帧");
MenuItem frameEnd = new MenuItem("设为结束帧");
contextMenu.getItems().addAll(frameBegin, frameEnd);
3.菜单项添加点击事件
frameBegin.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("设置开始帧");
}
});
frameEnd.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("设置结束帧");
}
});
关键是使用了setOnContextMenuRequested
,设置控件右击弹出菜单项。