Java 为什么组件不出现在屏幕的中心? 当用户运行程序时,我需要将场景的组成部分放在窗口中间,情况并非如此。我该如何解决这个问题? 场景似乎只出现在右侧,而不出现在中心。 我附加了一个我得到的输出图像 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); } }

Java 为什么组件不出现在屏幕的中心? 当用户运行程序时,我需要将场景的组成部分放在窗口中间,情况并非如此。我该如何解决这个问题? 场景似乎只出现在右侧,而不出现在中心。 我附加了一个我得到的输出图像 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); } },java,javafx,Java,Javafx,一种可能是,您的vbox只占用图像和hbox所需的空间。 尝试将vbox的高度和宽度设置为max balue,以便占用整个空间。您可以做的一件事是将窗格更改为堆栈窗格 触碰 欢迎光临 完整代码 触摸 import javafx.application.Application; import javafx.geometry.Rectangle2D; import javafx.scene.layout.StackPane; import javafx.stage.Screen; import ja

一种可能是,您的vbox只占用图像和hbox所需的空间。

尝试将vbox的高度和宽度设置为max balue,以便占用整个空间。

您可以做的一件事是将
窗格
更改为
堆栈窗格

触碰

欢迎光临

完整代码

触摸

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

我不明白将
窗格
传递给
WelcomeScene
?我将窗格传递给WelcomeScene是为了将每个场景写入不同的文件中,以便在一个文件中没有太多代码。谢谢!很好用@雅拉福:什么工作完美?你的意思是在其中一个答案上发表评论吗?如果是,请在分数下面打勾,将其标记为正确答案。哦,对不起!我想对另一个答案发表评论!但至少要感谢你的努力!
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();
        }
    }
}