0

我想弄清楚如何制作我的 VBox,只能达到 300 像素宽的大小,但我想在程序初始化时让它宽 250 像素,然后当用户点击全屏时,我想要它会扩展,但不一定会扩展它所拥有的整个空间。我希望它只达到 300 像素(并让里面的 3 个按钮做同样的事情),但我不知道该怎么做。我无法确定 PrefSize 和 CompSize 的实际含义和用途。任何帮助都会很棒。

我也遇到了与标签相同的问题,在 HBox 内,在 SplitPane 内,在 BorderPane 内。任何关于你所建议的为什么会起作用的解释都将帮助我解决未来的此类问题。谢谢

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MainStarUI extends Application {





    @Override
    public void start(Stage primaryStage){


        MenuBar mainMenuOne = addMenuBar();
        VBox leftVBoxOne = addVbox();
        //AnchorPane midPaneOne = addAnchorPane();
        //HBox topHBoxOne = addHBox();
        SplitPane midSplitPane = addSplitPane();

        BorderPane mainPane = new BorderPane();
        mainPane.setTop(mainMenuOne);
        mainPane.setLeft(leftVBoxOne);
        mainPane.setCenter(midSplitPane);

        primaryStage.setMinWidth(1440);
        primaryStage.setMinHeight(900);
        Scene mainScene = new Scene(mainPane);
        primaryStage.setScene(mainScene);
        primaryStage.show();

    }

    public MenuBar addMenuBar(){
        Menu menuOne = new Menu("File");
        Menu menuTwo = new Menu("Edit");
        Menu menuThree = new Menu("Help");
        Menu menuFour = new Menu("Exit");

        MenuItem menuItemOne = new MenuItem("File");
        MenuItem menuItemTwo = new MenuItem("Open");
        MenuItem menuItemThree = new MenuItem("Exit");

        menuOne.getItems().add(menuItemOne);
        menuOne.getItems().add(menuItemTwo);
        menuFour.getItems().add(menuItemThree);

        MenuBar mainMenuOne = new MenuBar();

        mainMenuOne.getMenus().add(menuOne);
        mainMenuOne.getMenus().add(menuTwo);
        mainMenuOne.getMenus().add(menuThree);
        mainMenuOne.getMenus().add(menuFour);

        mainMenuOne.maxHeight(25);
        mainMenuOne.minHeight(25);


        return mainMenuOne;

    }

    public VBox addVbox(){
        VBox leftVBox = new VBox();
        leftVBox.setMinWidth(300);
        leftVBox.setPrefWidth(300);

        leftVBox.setPadding(new Insets(15));
        leftVBox.setSpacing(20);
        leftVBox.setStyle("-fx-background-color: #336699;");

        Button firstButton = new Button("Ships, Components, Items & Weaponry");
        firstButton.setMinSize(270, 270);
        firstButton.setMaxSize(270, 270);

        Button secondButton = new Button("Trading, Mining, Refining & Commodities");
        secondButton.setMinSize(270, 270);
        secondButton.setMaxSize(270,270);

        Button thirdButton = new Button("Star Systems, Planets, Moons & Locations");
        thirdButton.setMinSize(270,270);
        thirdButton.setMaxSize(270, 270);

        leftVBox.getChildren().addAll(firstButton, secondButton, thirdButton);

        return leftVBox;
    }


    public HBox addHBox(){

        Image logoImage = new Image("SCImages/TaktikalLogo1.jpg");

        ImageView logoImageView = new ImageView();
        logoImageView.setImage(logoImage);
        logoImageView.setPreserveRatio(false);
        logoImageView.setFitWidth(160);
        logoImageView.setFitHeight(160);
        logoImageView.setSmooth(true);
        logoImageView.setCache(true);

        Label topLabel = new Label("STAR CITIZEN INFONET & DATABASE");
        topLabel.setFont(new Font("Arial", 48));
        topLabel.setTextFill(Color.WHITE);
        topLabel.setMinHeight(160);
        topLabel.setMaxHeight(160);


        HBox topHBox = new HBox();
        topHBox.setStyle("-fx-background-color: black");
        topHBox.setMinHeight(180);
        topHBox.setMaxHeight(180);
        topHBox.setPrefWidth(1090);
        topHBox.getChildren().addAll(logoImageView, topLabel);
        topHBox.setPadding(new Insets(10));
        topHBox.setSpacing(10);

        return topHBox;

    }

    public SplitPane addSplitPane(){

        HBox topHBoxOne = addHBox();
        AnchorPane anchorSplitPane = new AnchorPane();

        SplitPane mainSplitPane = new SplitPane();
        mainSplitPane.setOrientation(Orientation.VERTICAL);
        mainSplitPane.setDividerPosition(1, 200);
        mainSplitPane.setPrefSize(1090, 850);
        mainSplitPane.getItems().addAll(topHBoxOne, anchorSplitPane);


        return mainSplitPane;
    }

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

1 回答 1

0

实际上,我将我的 VBox 放在了 AnchorPane 中,并将其连接到锚点上,在我设置了我喜欢的高度和宽度后,一切都运行良好。

于 2021-02-03T06:04:40.420 回答