首页 > 解决方案 > 按钮的 Javafx BackgroundImage 不起作用

问题描述

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

标签: javajavafx

解决方案


正如@VGR 指出的那样,该链接未指向图像。您可能还希望指向固定资源位置(在您的应用程序或云中)中的图像。有一天,某人/您可以更改页面上的该图像,并且您的按钮将再次损坏。


推荐阅读