我试图在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
}
});
}
}
当我第一次尝试运行它时,它工作了,一个新的窗口打开了,中间有一个按钮,但后台是空白的。当我尝试将图像设置为窗口中的背景时,按下“开始”按钮,没有任何反应。
发布于 2019-04-11 22:36:33
一种更好的方法是使用Background
类,而不是尝试将ImageView
添加为BorderPane
的子级。
Image image = new Image("C:\\Users\\amali\\git\\inf101.v19.sem2\\inf101.v19.sem2\\src\\window\\battleshipbackground.jpg");
BackgroundSize size = new BackgroundSize(BackgroundSize.AUTO,
BackgroundSize.AUTO,
false,
false,
true,
false);
Background background = new Background(new BackgroundImage(image,
BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT,
BackgroundPosition.CENTER,
size));
newStack.setBackground(background);
发布于 2019-04-11 22:50:17
https://stackoverflow.com/questions/55635045
复制相似问题