java - 背景图像不工作 javaFX

标签 java javafx javafx-8

javafx 新手,我目前无法让我的图像成为我的背景,这可能是一些愚蠢的事情。这是代码。任何帮助表示赞赏。

package game;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.Background;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundPosition;
public class appgame extends Application {

    Button button;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Title of the Window");
        Pane p = new HBox();
        p.setPadding(new javafx.geometry.Insets(5,5,5,5));
        Image image = new Image("file:/home/rex/Documents/codes/java/bg1.jpg");

BackgroundImage backgroundImage = new BackgroundImage(image,BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);

Background background = new Background(backgroundImage);

        Scene scene = new Scene(p, 306, 460);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我无法将背景连接到场景,请有人帮助我将图像设置为场景背景。目前该代码仅显示一个空白舞台,没有背景图像。 提前致谢。

最佳答案

您忘记将背景添加到实例化 Pane (p)。

package appgame;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.stage.Stage;

import static javafx.scene.layout.BackgroundPosition.CENTER;
import static javafx.scene.layout.BackgroundRepeat.NO_REPEAT;
import static javafx.scene.layout.BackgroundRepeat.REPEAT;
import static javafx.scene.layout.BackgroundSize.*;

public class AppGame extends Application {

    private static final String BACKGROUND_PATH = "<path to background>";

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane p = new HBox();
        p.setPadding(new javafx.geometry.Insets(5,5,5,5));
        //Set your background!
        p.setBackground(new Background(new BackgroundImage(new Image(BACKGROUND_PATH), REPEAT, NO_REPEAT, CENTER, DEFAULT)));

        primaryStage.setTitle("Title of the Window");
        primaryStage.setScene(new Scene(p, 306, 460));
        primaryStage.show();
    }
}

这会导致(在我的文件系统上使用 png):

enter image description here

关于java - 背景图像不工作 javaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47473901/

相关文章:

java - Android 工作线程卡住主线程

javafx - 为什么即使 fxml 文件的路径正确,我也会收到 javafx.fxml.LoadException

JavaFX 打印

JavaFX : Why doesn't the setContent method of SwingNode with a JComponent parameter accept a JPanel?

java - 调整 AnchorPane 中标签的大小

java - 在单元测试中使用 SpringRunner 可以吗?

java - Selenium webdriver 正在寻找 Chrome.exe 的错误路径

javafx - 为什么 ExecutorService 不返回我的任务结果?

JavaFX:禁用 TableView 多列排序

java - 如何将 byte[] 转换为 ArrayList<CustomObjec>?