Моя графика javafx не показывает изображения на кнопках, которые я создал - PullRequest
0 голосов
/ 04 ноября 2019
package javafxskel;

import javafx.event.*;
import javafx.scene.image.*;
import javafx.application.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import static javafx.scene.control.ContentDisplay.GRAPHIC_ONLY;
import javafx.scene.image.Image;

Возможно, что-то не так с моим импортом. Я также пытался вставить filepath в new Image("")

public class JavaFXSkel extends Application {

    public void init() {
        System.out.println("Inside the innit method()");
    }

    public void start(Stage myStage) {

        myStage.setTitle("WINDOW");

        FlowPane rootNode = new FlowPane(10, 10);
        rootNode.setAlignment(Pos.CENTER);
        Scene myScene = new Scene(rootNode, 250, 250);
        myStage.setScene(myScene);

Я не знаю, есть ли еще какие-то подробности, которые вы должны знать, но этот код

    Image picture = new Image("file:pictureIcon.jpeg");
    Image save = new Image("file:saveIcon.jpeg");

    Label response = new Label("push a button");

    Button btnpicture = new Button("Picture", new ImageView(picture));
    Button btnsave = new Button("Save", new ImageView(save));

    btnpicture.setContentDisplay(GRAPHIC_ONLY);
    btnsave.setContentDisplay(GRAPHIC_ONLY);

    btnpicture.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent ae) {
            response.setText("Picture button");
        }

    });
    btnsave.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent ae) {
            response.setText("Save button");
        }

    });

    rootNode.getChildren().add(btnpicture);
    rootNode.getChildren().add(btnsave);
    rootNode.getChildren().add(response);

    myStage.show();

}
...