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

标签 java javafx

当用户运行程序时,我需要将场景的组件放置在窗口的中间,但事实并非如此。我该如何解决这个问题? 场景似乎只出现在右侧,而没有出现在中心。 我附上了我得到的输出的图像。

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

最佳答案

您可以做的一件事是将 Pane 更改为 StackPane

Touch

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

WelcomeScene

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

Full code

触摸

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

欢迎场景

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

enter image description here

关于java - 为什么组件没有出现在屏幕中央?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55950914/

相关文章:

java - 禁用的文本框在 jsp 中的页面刷新时丢失其值

java - 在JavaFX中,如何在不失去第一个阶段焦点的情况下打开另一个阶段?

java - 查找引用的 jar 的主类

3d - 将 3D 对象鼠标拖动移动限制到 JavaFX 中的平面

java - 检查圆是否完全包含圆

java - 如何重写子类中的方法并调用父类方法而不丢失数据

java - 如何强制 Windows 上的 Java 标准输出使用 "\n"行分隔符?

Javafx如何通过函数创建按钮?

javafx 由 : java. lang.UnsupportedOperationException 引起

java - 限制对其他 JFrame 的访问