【发布时间】:2019-09-02 05:45:37
【问题描述】:
我正在尝试将图像作为背景放在 JavaFX 场景中,但我的代码不起作用。
我试图在 java eclipse 中制作一个战舰游戏程序,但我遇到了图形问题。
公共类 WindowGUI 扩展应用程序{
Game game;
Image image;
public WindowGUI(Game game) {
this.game = game;
}
public static void main(String[] args) {
Game game = new Game();
new WindowGUI(game);
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Battleship");
image = new Image ("C:\\Users\\amali\\git\\inf101.v19.sem2\\inf101.v19.sem2\\src\\window\\battleshipbackground.jpg");
ImageView background = new ImageView(image);
Button startButton = new Button("START");
BorderPane newStack = new BorderPane();
newStack.getChildren().add(startButton);
newStack.getChildren().add(background);
stage.setScene(new Scene(newStack, 1300, 860));
stage.show();
startButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// START THE GAME
}
});
}
}
当我第一次尝试运行它时,它工作并打开了一个新窗口,中间有一个按钮,但 bakcground 是空白的。当我尝试在窗口中将图像设置为背景时,使用“开始”按钮,没有任何反应..
【问题讨论】:
-
观察
Image.exception属性,看看在加载图片时是否有任何错误。