仅在双击选项卡名称时调整选项卡大小

时间:2013-06-16 17:44:32

标签: javafx-2 javafx javafx-8

我有这个代码,无论我点击选项卡的哪个位置,它都会在点击两次时调整标签大小。

VBox stackedTitledPanes = createStackedTitledPanes();

        ScrollPane scroll = makeScrollable(stackedTitledPanes);

        final TabPane tabPane = new TabPane();
        final BorderPane mainPane = new BorderPane();

        final Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();

        tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
        // Create Tabs
        Tab tabA = new Tab();

        tabPane.setOnMouseClicked(new EventHandler<MouseEvent>()
        {
            private double sizeX, sizeY;
            private boolean first = true;

            @Override
            public void handle(MouseEvent me)
            {
                if (first)
                {
                    sizeX = mainPane.getWidth();
                    sizeY = mainPane.getHeight();
                    first = false;
                }

                if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 2 == 0)
                {
                    if (sizeX != mainPane.getWidth() || sizeY != mainPane.getHeight())
                    {
                        mainPane.setPrefSize(sizeX, sizeY);

                    }
                    else
                    {
                        mainPane.setPrefSize(primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
                    }
                }
            }
        });

        tabA.setText("Main Component");
        tabA.setContextMenu(makeTabContextMenu(tabA, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabA_stack = new StackPane();
        tabA_stack.setAlignment(Pos.CENTER);
        tabA_stack.getChildren().add(scroll);
        tabA.setContent(tabA_stack);
        tabPane.getTabs().add(tabA);

        Tab tabB = new Tab();
        tabB.setText("Second Component");
        tabB.setContextMenu(makeTabContextMenu(tabB, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabB_stack = new StackPane();
        tabB_stack.setAlignment(Pos.CENTER);
        tabB_stack.getChildren().add(new Label("Label@Tab B"));
        tabB.setContent(tabB_stack);
        tabPane.getTabs().add(tabB);

        Tab tabC = new Tab();
        tabC.setText("Last Component");
        tabC.setContextMenu(makeTabContextMenu(tabC, tabPane));  // Set right mouse click menu
        // Add something in Tab
        StackPane tabC_vBox = new StackPane();
        tabC_vBox.setAlignment(Pos.CENTER);
        tabC_vBox.getChildren().add(new Label("Label@Tab C"));
        tabC.setContent(tabC_vBox);
        tabPane.getTabs().add(tabC);

        mainPane.setCenter(tabPane);
        mainPane.setPrefSize(300, 500);
        //mainPane.setLayoutX(5);     // Horizontal Position
        mainPane.setLayoutY(32);    // Vertical Position

        scroll.setPrefSize(395, 580);
        scroll.setLayoutX(5);
        scroll.setLayoutY(32);

你能告诉我如何修改代码,只有当我双击选项卡名称时才可以调整选项卡的大小吗?

1 个答案:

答案 0 :(得分:1)

简化,

创建一个EventHandler

EventHandler eventHandler = new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent me)
            {
             if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
                if (mouseEvent.getClickCount() == 2) {
                 ...
                 ...


                 ...

              } }
            }
        }

并将此事件处理程序添加到每个选项卡的节点。

node.setOnMouseClicked(eventHandler);

这是你唯一的选择。