EventHandler进入EventHandler

时间:2014-03-20 10:20:42

标签: javafx javafx-2 javafx-8

我想把ContextMenu调用到EventHandler中。这段代码成功运作,但我遇到了一个问题。

bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
        {
            @Override
            public void handle(ContextMenuEvent event)
            {

                ContextMenu contextMenu = new ContextMenu();
                MenuItem item1 = new MenuItem("About");
                item1.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent e)
                    {
                        System.out.println("About");
                    }
                });
                MenuItem item2 = new MenuItem("Preferences");
                item2.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent e)
                    {
                        System.out.println("Preferences");
                    }
                });
                MenuItem item3 = new MenuItem("Close");
                item3.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent e)
                    {

                    }
                });
                contextMenu.getItems().addAll(item1, item2, item3);

                bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
                {
                    @Override
                    public void handle(ContextMenuEvent event)
                    {
                        contextMenu.show(bp, event.getScreenX(), event.getScreenY());
                        event.consume();
                    }
                });

                bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
                {
                    @Override
                    public void handle(MouseEvent event)
                    {
                        contextMenu.hide();
                    }
                });
                event.consume();
            }
        });

当我使用此事件处理程序加载Tab内容时,我必须在第一次单击两次才能调用上下文菜单。我想,如果这是EventHandler进入EventHandler的原因。我如何编辑代码,以便我可以使用一个EventHandler调用上下文菜单?

1 个答案:

答案 0 :(得分:0)

试试这个

bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
        {
            @Override
            public void handle(ContextMenuEvent event)
            {

                ContextMenu contextMenu = new ContextMenu();
                MenuItem item1 = new MenuItem("About");
                item1.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent e)
                    {
                        System.out.println("About");
                    }
                });
                MenuItem item2 = new MenuItem("Preferences");
                item2.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent e)
                    {
                        System.out.println("Preferences");
                    }
                });
                MenuItem item3 = new MenuItem("Close");
                item3.setOnAction(new EventHandler<ActionEvent>()
                {
                    @Override
                    public void handle(ActionEvent e)
                    {

                    }
                });
                contextMenu.getItems().addAll(item1, item2, item3);

                contextMenu.show(bp, event.getScreenX(), event.getScreenY());

                bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
                {
                    @Override
                    public void handle(MouseEvent event)
                    {
                        contextMenu.hide();
                    }
                });
                event.consume();
            }
        });