JavaFX TitledPane固定大小

时间:2019-06-04 12:08:32

标签: java javafx

我有一个窗口,其中有两个TitledPaneVBox中。

TitledPane根据窗口大小调整宽度,但是我希望TitledPane具有固定大小。

主类:

public static void main(String[] ar){ launch(ar); }

@Override
public void start(Stage primaryStage){

    ControlPane controlPane = new ControlPane(); //extends TitledPane -> content = HBox -> Button + Pane + Button
    TitledPane logView = new LogViewPane(); //extends TitledPane -> content = HBox -> TextField + Button + Button

    VBox vbox = new VBox();
    vbox.setPadding(new Insets(10));

    VBox.setMargin(controlPane, new Insets(0, 0, 10, 0));

    vbox.getChildren().addAll(controlPane, logView);

    var classLoader = getClass().getClassLoader();

    Scene scene = new Scene(vbox);
    scene.getStylesheets().addAll(classLoader.getResource("control-status-style.css").toExternalForm(),
                                    classLoader.getResource("style.css").toExternalForm());

    primaryStage.setScene(scene);
    primaryStage.sizeToScene();

    primaryStage.show();

}

ControlPane类:

public class ControlPane extends TitledPane{

    private final Button start, close;
    private final Pane status;

    public ControlPane(){

        super("CONTROL", null);

        HBox hbox = new HBox();
        hbox.setAlignment(Pos.CENTER_LEFT);
        hbox.setPadding(new Insets(10));

        start = new Button("START");
        close = new Button("CLOSE");

        status = new Pane();
        status.setMinWidth(10);
        status.setMaxWidth(10);

        HBox.setMargin(start, new Insets(0, 10, 0, 0));
        HBox.setMargin(status, new Insets(0, 10, 0, 0));
        HBox.setMargin(close, new Insets(0));

        hbox.getChildren().addAll(start, status, close);

        this.setCollapsible(false);
        this.setContent(hbox);

    }

}

LogViewPane类:

public class LogViewPane extends TitledPane{

    public LogViewPane(){

        super("LOG", null);

        HBox hbox = new HBox();
        hbox.setAlignment(Pos.CENTER_LEFT);
        hbox.setPadding(new Insets(10));

        TextField inputDate = new TextField();
        inputDate.setMinWidth(100);
        inputDate.setMaxWidth(100);

        Button buttonLog = new Button("LOG");
        Button buttonChart = new Button("CHART");

        HBox.setMargin(inputDate, new Insets(0, 10, 0, 0));
        HBox.setMargin(buttonLog, new Insets(0, 10, 0, 0));
        HBox.setMargin(buttonChart, new Insets(0));

        hbox.getChildren().addAll(inputDate, buttonLog, buttonChart);

        this.setCollapsible(false);
        this.setContent(hbox);

    }

}

它看起来像这样:

我希望它看起来像这样:

如何在不使用set(Min / Max)Size的情况下防止扩展/收缩TitledPane?

0 个答案:

没有答案