【发布时间】: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;
}
提前致谢
【问题讨论】: