JavaFX,可调整大小的容器,位于不可调整大小的容器旁边

时间:2014-05-05 15:37:19

标签: java javafx vbox hbox

我想在屏幕上显示一些内容,但我不太确定应该使用哪种布局系统。

从屏幕右侧我想要一个300px宽和屏幕高度的容器。

在右侧容器占据其位置后,左侧容器将填充屏幕的其余部分。

这是一个大容器,它已经调整了优先级,然后是另一个300px宽的容器。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

containme

生成上图所示布局的示例代码

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;

/**
 * Creates a full screen display with two containers.
 * The right container is fixed to 300 pixels wide.
 * The left container fills the remaining available space.
 */
public class ContainerSample extends Application {
    @Override public void start(final Stage stage) throws Exception {
        // create a resizable pane.
        final StackPane left = new StackPane();
        left.setStyle("-fx-background-color: lightblue;");
        left.setPrefWidth(500);

        // create a pane with a 300 pixel fixed width.
        final StackPane right = new StackPane();
        right.setStyle("-fx-background-color: palegreen;");
        right.setPrefWidth(300);
        right.setMinWidth(StackPane.USE_PREF_SIZE);
        right.setMaxWidth(StackPane.USE_PREF_SIZE);

        // layout the left and right pane.
        final HBox layout = new HBox(left, right);

        // grow the left pane width to fill available space.
        HBox.setHgrow(left, Priority.ALWAYS);

        // exit the application on mouse click.
        layout.setOnMouseClicked(event -> stage.hide());

        // uncomment to disallow exiting the full screen mode.
        //stage.setFullScreenExitHint("");
        //stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);

        // display the main application screen in full screen mode.
        stage.setFullScreen(true);
        stage.setScene(new Scene(layout));
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
} 

答案 1 :(得分:0)

或在FXML中:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane style="-fx-background-color: lightblue;" HBox.hgrow="ALWAYS" />
      <AnchorPane prefWidth="300.0" style="-fx-background-color: palegreen;" HBox.hgrow="NEVER" />
   </children>
</HBox>