简体   繁体   English

为什么组件没有出现在屏幕中央?

[英]Why are the components not appearing in the center of the screen?

I need the components of the scene to be placed in the middle of the window when the user runs the program and this is not the case.当用户运行程序时,我需要将场景的组件放置在窗口的中间,但事实并非如此。 How may I solve this?我该如何解决这个问题? The scene only seems to appear at the right but not at the center.场景似乎只出现在右侧,而不出现在中心。 I attached an image of the output I am getting.我附上了我得到的输出的图像。

public class WelcomeScene extends Scene{
    public WelcomeScene(Pane pane, double width, double height){
        super(pane,width,height);
        setFill(Color.PINK);
        VBox vbox = new VBox();
        Label label = new Label("Welcome to the typing\n practice world");
        label.setFont(new Font("Cambria", 32));
        label.setStyle("-fx-text-alignment: center;");
        label.setTextFill(Color.RED);
        label.centerShapeProperty().bind(pane.centerShapeProperty());

        HBox hbox = new HBox();
        Button StartTypingBtn = new Button("Start typing");
        Button showFingurePosition = new Button("Check fingures prositions");
        Button checkImprovement = new Button("Check Improvement");
        hbox.setSpacing(10);
        hbox.setAlignment(Pos.CENTER);

        hbox.getChildren().add(StartTypingBtn);
        hbox.getChildren().add(showFingurePosition);
        hbox.getChildren().add(checkImprovement);

        vbox.getChildren().addAll(label, new 
            ImageView("photos/hands_email.gif"),
                hbox);
        vbox.setAlignment(Pos.CENTER);
        pane.getChildren().add(vbox);
        pane.setCenterShape(true);
    }
}

public class Touch extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
        primaryStage.setScene(new WelcomeScene(new Pane(),
                screenBounds.getWidth(),screenBounds.getHeight()));
        primaryStage.show();
    }
    public static void main(String [] args) {
        Application.launch(args);
    }
}

enter image description here在此处输入图片说明

One thing you can do is change Pane to StackPane .您可以做的一件事是将Pane更改为StackPane

Touch触碰

primaryStage.setScene(new WelcomeScene(new StackPane(),
            screenBounds.getWidth(), screenBounds.getHeight()));

WelcomeScene欢迎现场

 public WelcomeScene(StackPane pane, double width, double height)

Full code完整代码

Touch触碰

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Touch extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
        primaryStage.setScene(new WelcomeScene(new StackPane(),
                screenBounds.getWidth(), screenBounds.getHeight()));
        primaryStage.show();
    }

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

WelcomeScene欢迎现场

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

public class WelcomeScene extends Scene
{
    public WelcomeScene(StackPane pane, double width, double height)
    {
        super(pane, width, height);
        try {
            setFill(Color.PINK);
            VBox vbox = new VBox();
            Label label = new Label("Welcome to the typing\n practice world");
            label.setFont(new Font("Cambria", 32));
            label.setStyle("-fx-text-alignment: center;");
            label.setTextFill(Color.RED);
            label.centerShapeProperty().bind(pane.centerShapeProperty());

            HBox hbox = new HBox();
            Button StartTypingBtn = new Button("Start typing");
            Button showFingurePosition = new Button("Check fingures prositions");
            Button checkImprovement = new Button("Check Improvement");
            hbox.setSpacing(10);
            hbox.setAlignment(Pos.CENTER);

            hbox.getChildren().add(StartTypingBtn);
            hbox.getChildren().add(showFingurePosition);
            hbox.getChildren().add(checkImprovement);

            vbox.getChildren().addAll(label, new ImageView(new Image(new FileInputStream("photos/hands_email.gif"))), hbox);
            vbox.setAlignment(Pos.CENTER);
            pane.getChildren().add(vbox);
            pane.setCenterShape(true);
        }
        catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}

在此处输入图片说明

One possability could be that your vbox takes only the space it needs for the image and the hbox.一种可能性是您的 vbox 仅占用图像和 hbox 所需的空间。
Try to set the hight and wide of the vbox to max balue so it takes the whole space.尝试将 vbox 的高度和宽度设置为 max balue,以便占用整个空间。

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

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