按钮的Javafx BackgroundImage不起作用

时间:2019-07-11 19:19:07

标签: java javafx

我正在尝试在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;
    }
}

1 个答案:

答案 0 :(得分:0)

正如@VGR所指出的,链接未指向图像。您可能还需要指向固定资源位置(在应用程序或云中)的映像。一天,某人/您可以在页面上更改该图像,然后您的按钮将再次断开。