[英]Divide stage into 2 gridpanes JavaFX
因此,我试图在左侧显示文本,在右侧显示按钮,文本应具有恒定的大小,并且按钮应调整大小以填充窗口的其余部分。
到目前为止,这是我的结果:
我不想在按钮上输入文字,而是希望它们共享整个窗口。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
GridPane buttons = new GridPane();
GridPane textGrid = new GridPane();
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
Button button1 = new Button();
Button button2 = new Button();
Button button3 = new Button();
Button button4 = new Button();
Button button5 = new Button();
button1.setText("Button1");
button2.setText("Button4");
button3.setText("Button3");
button4.setText("Button4");
button5.setText("Button5");
TextArea text1 = new TextArea();
text1.setText("Test");
text1.setPrefSize(100, 100);
button1.prefWidthProperty().bind(buttons.widthProperty());
button2.prefWidthProperty().bind(buttons.widthProperty());
button3.prefWidthProperty().bind(buttons.widthProperty());
button4.prefWidthProperty().bind(buttons.widthProperty());
button5.prefWidthProperty().bind(buttons.widthProperty());
button1.prefHeightProperty().bind(buttons.heightProperty());
button2.prefHeightProperty().bind(buttons.heightProperty());
button3.prefHeightProperty().bind(buttons.heightProperty());
button4.prefHeightProperty().bind(buttons.heightProperty());
button5.prefHeightProperty().bind(buttons.heightProperty());
buttons.addColumn(0, button1, button2, button3, button4, button5);
textGrid.addColumn(0, text1);
Scene scene = new Scene(root, 280, 180);
root.getChildren().addAll(buttons, textGrid);
buttons.setAlignment(Pos.TOP_RIGHT);
textGrid.setAlignment(Pos.TOP_LEFT);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
通常,让布局窗格处理布局管理比尝试通过绑定管理布局更好。
这是一个示例:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.stream.IntStream;
public class Main extends Application {
private static final int N_BUTTONS = 5;
@Override
public void start(Stage stage) {
VBox buttonLayout = new VBox(
10,
IntStream.range(0, N_BUTTONS)
.mapToObj(this::createButton)
.toArray(Button[]::new)
);
HBox.setHgrow(buttonLayout, Priority.ALWAYS);
TextArea textArea = new TextArea("Test");
textArea.setPrefWidth(100);
textArea.setMaxWidth(TextArea.USE_PREF_SIZE);
textArea.setMinWidth(TextArea.USE_PREF_SIZE);
HBox layout = new HBox(10, textArea, buttonLayout);
layout.setPadding(new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
private Button createButton(int i) {
Button button = new Button("Button " + i);
// button.setMaxWidth(Double.MAX_VALUE);
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
VBox.setVgrow(button, Priority.ALWAYS);
return button;
}
public static void main(String[] args) {
launch(args);
}
}
根据示例,我将指出以下几点:
IntStream
范围与map和toArray
,但是您可以使用标准的for循环(可能更容易理解)执行相同的操作。 HBox.setHgrow(buttonLayout, Priority.ALWAYS);
告诉Box始终将Box中的所有额外空间分配给buttonLayout,以便按钮将填充所有剩余区域。 在各个节点上设置约束以根据需要调整大小,例如,以下代码为textArea建立了固定宽度,该宽度不会改变(如果需要,您可以使用类似的代码来建立固定高度):
textArea.setPrefWidth(100); textArea.setMaxWidth(TextArea.USE_PREF_SIZE); textArea.setMinWidth(TextArea.USE_PREF_SIZE);
某些控件会自动将自身扩展到其最大大小以外,默认情况下不会启用按钮,请使用以下代码来启用此行为(如果您只想扩展宽度而不是高度,则只能设置maxWidth而不是maxSize) :
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
与其像本例中那样在代码中定义布局,不如使用SceneBuilder之类的工具直观地创建场景并将布局保存为FXML文件 ,以使布局与代码分离(类似地,将任何样式都放置在外部CSS中文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.