我有一个tabpane并且在选项卡中我需要有一个布局,其中我有四个子窗口(窗格?),应该有最大化和最小化的控件。 最大化(可能是其原始大小的两倍),它不应该占用整个窗口大小,它应该相对于它所属的选项卡。哪个面板最适合此用例?
答案 0 :(得分:0)
这是一个建议,使用GridPane
并通过操纵行和列索引以及行和列跨度来最大化来自一个“单元格”的内容。
import java.util.Random;
import java.util.function.IntFunction;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
public class ExpandingPanes extends Application {
private final Random rng = new Random();
@Override
public void start(Stage primaryStage) {
TabPane tabs = new TabPane();
for (int i = 1 ; i <= 4 ; i++) {
final String tabText = "Tab "+ i ;
tabs.getTabs().add(new Tab(tabText, createGridPane(x -> tabText+" : Pane "+x, 2, 2)));
}
BorderPane root = new BorderPane(tabs);
root.setBottom(new TextField());
Scene scene = new Scene(root, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
private GridPane createGridPane(IntFunction<String> labelText, int numRows, int numCols) {
GridPane grid = new GridPane();
for (int row = 0 ; row < numRows; row++) {
RowConstraints rc = new RowConstraints();
rc.setFillHeight(true);
rc.setVgrow(Priority.ALWAYS);
rc.setPercentHeight(100.0 / numRows);
grid.getRowConstraints().add(rc);
}
for (int col = 0 ; col < numCols; col++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setFillWidth(true);
cc.setHgrow(Priority.ALWAYS);
cc.setPercentWidth(100.0 / numCols);
grid.getColumnConstraints().add(cc);
}
for (int i = 0; i < numRows * numCols; i++) {
int row = i / numRows ;
int col = i % numCols ;
Pane tile = createPane(col, row, numCols, numRows, labelText.apply(i));
grid.add(tile, col, row);
}
return grid ;
}
private Pane createPane(int col, int row, int numCols, int numRows, String text) {
BorderPane content = new BorderPane();
content.setCenter(new Label(text));
ToggleButton maximize = new ToggleButton();
maximize.textProperty().bind(Bindings.when(maximize.selectedProperty()).then("Restore").otherwise("Maximize"));
maximize.selectedProperty().addListener((obs, wasMax, isNowMax) -> {
if (isNowMax) {
GridPane.setColumnIndex(content, 0);
GridPane.setRowIndex(content, 0);
GridPane.setColumnSpan(content, numCols);
GridPane.setRowSpan(content, numRows);
content.toFront();
} else {
GridPane.setColumnIndex(content, col);
GridPane.setRowIndex(content, row);
GridPane.setColumnSpan(content, 1);
GridPane.setRowSpan(content, 1);
}
});
BorderPane.setMargin(maximize, new Insets(5));
BorderPane.setAlignment(maximize, Pos.CENTER_RIGHT);
content.setTop(maximize);
content.setStyle(randomColor());
return content ;
}
private String randomColor() {
return String.format("-fx-background-color: -fx-background; -fx-background: rgb(%d, %d, %d);",
rng.nextInt(256), rng.nextInt(256), rng.nextInt(256));
}
public static void main(String[] args) {
launch(args);
}
}