如何创建弹出菜单 [英] How to create popup menu

查看:85
本文介绍了如何创建弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javafx的新手.我想在鼠标右键上显示弹出菜单.我找到了一个教程此处

I am newbie on javafx. I want to show popup menu on Right Mouse click. I find one tutorial Here and Here but don't understand. I want to create popup menu which show in image on this link.

现在我正在创建舞台,但是我不想舞台.我需要显示一个弹出菜单,该菜单在右键单击时显示,并在我单击任意位置时关闭.

Right now i am creating stage but i don't want stage. I need to show popup menu which show on right click and close when i click anywhere.

这是我在其中使用舞台的代码,但我需要像上面的链接那样操作弹出菜单.

Here is my code in which i am using stage but i need to ope popup menu like above link.

 public void MouseClickedOnTree(MouseEvent event) {
if (event.isSecondaryButtonDown()) {

        System.out.println("secondary press");
        final Stage optionstage = new Stage();

        VBox vBox = new VBox(5);
        vBox.setMinHeight(50);
        vBox.setMinWidth(50);
        Button btnNewTestBed = new Button("New Testbed");
        btnNewTestBed.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                     optionstage.close();
                    stage.show();
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        });
        Button btnOpenTestbed = new Button("Open Testbed");
        btnOpenTestbed.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                optionstage.close();

            }
        });
        optionstage.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent t) {
                if (t.getCode() == KeyCode.ESCAPE) {
                    System.out.println("click on escape");
                    //Stage sb = (Stage) label.getScene().getWindow();//use any one object
                    if(optionstage.isShowing())
                        optionstage.close();
                }
            }
        });

        vBox.getChildren().addAll(btnNewTestBed, btnOpenTestbed);
        optionstage.setScene(new Scene(vBox, 100, 100));
        //stage.setScene(new Scene(new Group(new Text(50,50, "my second window")))); 
        optionstage.setX(event.getSceneX());
        optionstage.setY(event.getScreenY());
        optionstage.initStyle(StageStyle.UNDECORATED);
        optionstage.show();

    }

请提供任何链接或参考给我.

Please provide me any link or reference.

推荐答案

您的代码上下文不是很清楚:这是否在事件处理程序中?如果是这样,事件处理程序是做什么用的?如果不是,那么开头语句中的event是什么?

The context of your code is not very clear: is this inside an event handler? If so, what's it an event handler for? If not, what is event in the opening if statement?

您提供的两个链接非常复杂;在JavaFX中(与Swing不同),您仅应考虑将现有Node类的子类用于相当高级的用例.您仅需要创建弹出菜单就不需要这种复杂性.

The two links you provide are pretty complex; in JavaFX (unlike Swing) you should only consider subclassing existing Node classes for pretty advanced use cases. You don't need this level of complexity just to create a popup menu.

创建弹出菜单的最简单方法是使用Control(或子类).您只需要创建一个ContextMenu,向其中添加MenuItem,然后在您的控件上将其设置为contextMenu:

The easiest way to create a popup menu is for a Control (or subclass); you just need to create a ContextMenu, add MenuItems to it, and set it as the contextMenu on your control:

TextField textField = new TextField("Type Something"); // we will add a popup menu to this text field
final ContextMenu contextMenu = new ContextMenu();
MenuItem cut = new MenuItem("Cut");
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
contextMenu.getItems().addAll(cut, copy, paste);
cut.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Cut...");
    }
});
// ...
textField.setContextMenu(contextMenu);

如果要在不是Control的节点上使用ContextMenu(例如,PaneShape),则没有setContextMenu(...)方法,因此您只需要多做一些工作:

If you want to use a ContextMenu on a Node that is not a Control (a Pane or a Shape, for example), you don't have a setContextMenu(...) method, so you just need a little more work:

final AnchorPane pane = new AnchorPane();
// fill pane with nodes, etc
// create context menu and menu items as above
pane.setOnMousePressed(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        if (event.isSecondaryButtonDown()) {
            contextMenu.show(pane, event.getScreenX(), event.getScreenY());
        }
    }
});

有关ContextMenu的信息,请参见 Javadocs .有关更多详细信息,请参见href ="http://docs.oracle.com/javafx/2/ui_controls/menu_controls.htm#sthref209">教程.

See the Javadocs for ContextMenu or the tutorial for more details.

这篇关于如何创建弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆