简体   繁体   English

按钮的Javafx BackgroundImage不起作用

[英]Javafx BackgroundImage for button is not working

I am trying to create a basic tic-tac-toe game in javafx. 我正在尝试在javafx中创建一个基本的井字游戏。 I ended up getting the basics to work, however I wanted to a.) change the background of the button to something and b.) add a win detection system(different story). 我最终获得了基础知识,但我想a。)将按钮的背景更改为某些内容,然后b。)添加获胜检测系统(不同的故事)。 When trying to put in the image, however, it would not show the image, it would just display a white blank screen. 但是,当尝试放入图像时,它不会显示图像,而只会显示白色的空白屏幕。 Why and how is this happening? 为什么以及如何发生?

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);
                }

            }

        }
    }

}

Tic Class: 抽认班:

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;
    }
}

As @VGR pointed, the link is not pointing to an image. 正如@VGR所指出的,链接未指向图像。 You also might want to point to an image in a fixed resource location (in your application or cloud). 您可能还需要指向固定资源位置(在应用程序或云中)的映像。 One day somebody/you can change that image on the page and your button will break again. 一天,某人/您可以在页面上更改该图像,然后您的按钮将再次断开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM