【问题标题】:TabPane shrink to biggest childTabPane 缩小到最大的孩子
【发布时间】:2017-01-23 12:20:06
【问题描述】:

我正在努力完成这项工作。基本上我有 2 个标签。最初,其中一个的内容假设为 100px 大(我事先不知道大小。这只是一个示例),另一个没有内容。 在某些事件之后,我在之前的空选项卡中添加了一些东西,比如 200 像素的高度。

听到一切都很好。 TabPane 正确调整大小(在我调用 requestLayout() 之后)并适合新内容。

现在,如果我删除 200 像素的内容,我希望选项卡窗格恢复到 100 像素高,即最大的项目仍留在选项卡上,但它仍保持在 200 像素,给我留下了很多空白空间。有人可以告诉我如何解决这个问题吗?

这是我的演示代码:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TabPaneTest extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        TabPane tabPane = new TabPane();
        Tab tab1 = new Tab("Test 1");
        Tab tab2 = new Tab("Test 2");

        final Pane tab2Content = new Pane();
        tab2Content.getStyleClass().add("tab2Content");
        tab2.setContent(tab2Content);

        tabPane.getTabs().addAll(tab1, tab2);

        Button addSquare = new Button("Add");
        addSquare.setOnAction(event -> {
            final Pane tab1Content = new Pane();
            tab1Content.getStyleClass().add("tab1Content");

            tab1.setContent(tab1Content);

            tabPane.requestLayout();
        });

        Button removeSquare = new Button("Remove");
        removeSquare.setOnAction(event -> {
            tab1.setContent(null);
            tabPane.requestLayout();
        });
        HBox buttons = new HBox(addSquare, removeSquare);


        VBox vBox = new VBox(tabPane, buttons);
        Scene scene = new Scene(vBox, 500, 400);
        scene.getStylesheets().add(LargeTooltipSample.class.getResource("tabPaneTest.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

这是我的 CSS(只是为了更好地可视化内容):

.tab2Content{
    -fx-background-color: red;
    -fx-max-height: 100;
    -fx-pref-height: 100;
    -fx-max-width: 100;
    -fx-pref-width: 100;
}

.tab1Content{
    -fx-background-color: green;
    -fx-max-height: 200;
    -fx-pref-height: 200;
    -fx-max-width: 200;
    -fx-pref-width: 200;
}

提前致谢

【问题讨论】:

    标签: javafx javafx-8


    【解决方案1】:

    试试这个:

    @Override
    public void start(Stage primaryStage) throws Exception {
            TabPane tabPane = new TabPane();
            Tab tab1 = new Tab("Test 1");
            Tab tab2 = new Tab("Test 2");
    
            final Pane tab2Content = new Pane();
            final Pane tab1Content = new Pane();
    
            tab2Content.getStyleClass().add("tab2Content");
            tab2.setContent(tab2Content);
    
            tabPane.getTabs().addAll(tab1, tab2);
    
            Button addSquare = new Button("Add");
            Button removeSquare = new Button("Remove");
            HBox buttons = new HBox(addSquare, removeSquare);
            VBox vBox = new VBox(tabPane, buttons);
    
    
            double[] tabPaneOriginalHeight = new double[1];//Used to hold tabPane's original height.
    
            addSquare.setOnAction(event -> {  
    
            System.out.println(tabPane.getHeight());
            tab1Content.getStyleClass().add("tab1Content");
            tab1.setContent(tab1Content);    
            System.out.println(tabPane.getHeight());
            tabPane.setPrefHeight(200);//sets the height to 200 after adding tabl1 content
            tabPane.requestLayout();
    
    
        });
    
    
        removeSquare.setOnAction(event -> {
            System.out.println(tabPane.getHeight());
    
            tab1Content.getStyleClass().remove("tab1Content");
            tab1.setContent(tab1Content);
            System.out.println(tabPane.getHeight());
            tabPane.setPrefHeight(100);//sets the height to 100 after removing tab1 content
            tabPane.requestLayout(); 
        });
    
    
    
    
            Scene scene = new Scene(vBox, 500, 400);
            scene.getStylesheets().add(JavaFXApplication16.class.getResource("tabPaneTest.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    

    【讨论】:

    • 绑定会有更好的办法。
    • 这确实适用于本示例,但想象以下场景:200px 部分已经到位。然后你删除它,只留下 100px 的那个。使用您的方法不会重新缩放到 100px,因为“原始高度”是 200px 而不是 100px :(
    • 好吧,而不是 tabPane.setPrefHeight(tabPaneOriginalHeight[0]);试试 tabPane.setPrefHeight(100);
    • 我在 Netbeans 中对其进行了测试。它似乎工作正常。符合你的标准吗?
    • 很遗憾不是塞德里克。您正在使问题适应给定的示例。我需要创建一些可以更广泛地工作的东西,即我不知道我想要恢复到的像素的大小。理想情况下,我会检查每个标签内容并查看最高的项目,但我事先不知道这一点
    猜你喜欢
    • 1970-01-01
    • 2015-03-26
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多