[英]Javafx BackgroundImage for button is not working
我正在嘗試在javafx中創建一個基本的井字游戲。 我最終獲得了基礎知識,但我想a。)將按鈕的背景更改為某些內容,然后b。)添加獲勝檢測系統(不同的故事)。 但是,當嘗試放入圖像時,它不會顯示圖像,而只會顯示白色的空白屏幕。 為什么以及如何發生?
package TicMeOff;
/**
* @author camper
*/
import TicMeOff.Program.ProcessClick;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.geometry.Pos;
public class Fx extends Application{
Image image2 = new Image("https://ibb.co/YNrvYZQ");
private Tic[][] tics = new Tic[3][3];
private int tracker = 1;
BackgroundImage backgroundImage = new BackgroundImage(image2,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
Background background = new Background(backgroundImage);
public void setTiles() {
for(int x = 0; x < tics.length; x++) {
for(int y = 0; y < tics[0].length; y++) {
tics[x][y] = new Tic();
tics[x][y].setPrefSize(300, 300);
}
}
}
public static void main(String[] args) {
Application.launch(args);
}
public void start(Stage primaryStage) {
setTiles();
BorderPane pane = new BorderPane();
GridPane buttonPane = new GridPane();
for(int x = 0; x < tics.length; x++) {
for(int y = 0; y < tics.length; y++) {
tics[x][y].setOnAction(new ProcessClick());
buttonPane.add(tics[x][y], x + 1,y + 1);
}
}
pane.setPrefSize(900, 900);
pane.setCenter(buttonPane);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
private class ProcessClick implements EventHandler<ActionEvent> {
public void handle(ActionEvent e) {
Tic b = (Tic) e.getSource();
for(int x = 0; x < tics.length; x++) {
for(int y = 0; y < tics.length; y++) {
tics[x][y].setBackground(background);
}
}
if ((b.getbuttonTracker()) == 0) {
if (tracker == 1) {
b.setText("X");
tracker = -1;
b.setbuttonTracker(1);
} else if (tracker == -1) {
b.setText("O");
tracker = 1;
b.setbuttonTracker(1);
}
}
}
}
}
抽認班:
package TicMeOff;
import javafx.application.Application;
import javafx.scene.control.Button;
public class Tic extends Button {
public int buttonTracker = 0;
public int getbuttonTracker() {
return this.buttonTracker;
}
public void setbuttonTracker(int buttonTracker) {
this.buttonTracker = buttonTracker;
}
}
正如@VGR所指出的,鏈接未指向圖像。 您可能還需要指向固定資源位置(在應用程序或雲中)的映像。 一天,某人/您可以在頁面上更改該圖像,然后您的按鈕將再次斷開。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.