新手到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();
}
}
我无法将我的背景连接到我的场景,请一些人帮助我将我的图像设置为我的场景背景 . 目前代码只显示一个空白阶段,没有背景图像 . 提前致谢 .
1 回答
您忘记将背景添加到实例化的窗格(p) .
这导致(在我的文件系统上使用png):