简体   繁体   English

将阶段分为2个网格窗格JavaFX

[英]Divide stage into 2 gridpanes JavaFX

So Im trying to have text on the left and buttons on the right, text should have constant size and buttons should resize to fill the rest of the window. 因此,我试图在左侧显示文本,在右侧显示按钮,文本应具有恒定的大小,并且按钮应调整大小以填充窗口的其余部分。

Here is my result so far: 到目前为止,这是我的结果:

至今...

I dont want my text over buttons, I want them to share the whole window. 我不想在按钮上输入文字,而是希望它们共享整个窗口。

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);
    }

}

It is usually better to let the layout panes handle the layout management rather than trying to manage the layout through bindings. 通常,让布局窗格处理布局管理比尝试通过绑定管理布局更好。

Here is a sample: 这是一个示例:

布局

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);
    }
}

Here are a couple of things I would point out based upon the sample: 根据示例,我将指出以下几点:

  1. As the buttons are so similar, create the buttons in a loop rather than individually in code. 由于按钮是如此相似,因此请在循环中创建按钮,而不是在代码中单独创建按钮。 I use an IntStream range with a map and a toArray , but you could do the same thing with a standard for loop (which may be easier to understand). 我将IntStream范围与map和toArray ,但是您可以使用标准的for循环(可能更容易理解)执行相同的操作。
  2. Use combinations of standard layout panes to achieve your layout. 使用标准布局窗格的组合来实现您的布局。 For example the buttons are vertically spaced, so put them in a VBox, the text and the buttons are horizontal to each other, so use a HBox. 例如,按钮垂直间隔,因此将它们放在VBox中,文本和按钮彼此水平,因此使用HBox。
  3. Use constraints on the layouts to massage them into performing the layout you like, for example, HBox.setHgrow(buttonLayout, Priority.ALWAYS); 在布局上使用约束条件,以约束它们执行所需的布局,例如HBox.setHgrow(buttonLayout, Priority.ALWAYS); tells the Box to always assign any extra additional space in the Box to the buttonLayout so that the buttons will fill any remaining area. 告诉Box始终将Box中的所有额外空间分配给buttonLayout,以便按钮将填充所有剩余区域。
  4. Set constraints on the individual nodes to size them how you wish, for example the following code establishes a fixed width for the textArea, which will not vary (you could similar code to establish a fixed height if you wished): 在各个节点上设置约束以根据需要调整大小,例如,以下代码为textArea建立了固定宽度,该宽度不会改变(如果需要,您可以使用类似的代码来建立固定高度):

     textArea.setPrefWidth(100); textArea.setMaxWidth(TextArea.USE_PREF_SIZE); textArea.setMinWidth(TextArea.USE_PREF_SIZE); 
  5. Some controls will automatically expand themselves beyond their max size, buttons do not by default, to enable this behavior use the following code (if you only wanted the width to expand and not the height then you would only set the maxWidth rather than the maxSize): 某些控件会自动将自身扩展到其最大大小以外,默认情况下不会启用按钮,请使用以下代码来启用此行为(如果您只想扩展宽度而不是高度,则只能设置maxWidth而不是maxSize) :

     button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
  6. Rather than defining layouts in code as in this example, instead use a tool such as SceneBuilder to create the scene visually and save the layout as an FXML file , so that the layout is separated from your code (similarly place any styling in an external CSS file). 与其像本例中那样在代码中定义布局,不如使用SceneBuilder之类的工具直观地创建场景并将布局保存为FXML文件 ,以使布局与代码分离(类似地,将任何样式都放置在外部CSS中文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM