为什么我的按钮图像集图形方法没有显示



我的 bin 文件夹中有图像文件,但是当我运行程序时,我所拥有的只是我的按钮选项,这些选项没有被我的图像替换。我认为这与文件有关:在图像前面,但我不确定。这些图像实际上并不是一个移动的gif(不知道为什么它被称为gif)。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception {
        HBox root = new HBox();
        Button btnKings;
        Button btnDucks;
        Button btnSharks;
        Button btnBlues;
        btnKings = new Button("Kings");
        btnDucks = new Button("Ducks");
        btnSharks = new Button("Sharks");
        btnBlues = new Button("Blues");
        Image imgKings = new Image("file:kings.jpg");
        btnKings.setGraphic(new ImageView(imgKings));
        Image imgDucks = new Image("file:ducks.gif");
        btnDucks.setGraphic(new ImageView(imgDucks));
        Image imgSharks = new Image("file:sharks.gif");
        btnSharks.setGraphic(new ImageView(imgSharks));
        Image imgBlues = new Image("file:blues.gif");
        btnBlues.setGraphic(new ImageView(imgBlues));
        HandleButtonClick clickEvent = new HandleButtonClick();
        btnKings.setOnAction(clickEvent);
        btnDucks.setOnAction(new HandleButtonClick("You clicked the ducKS!"));
        btnKings.setOnAction(new HandleButtonClick("You clicked the Kings"));
        btnSharks.setOnAction(new HandleButtonClick("You clicked the sharks"));
        btnBlues.setOnAction(new HandleButtonClick("You clicked the blues"));
        root.getChildren().add(btnKings);
        root.getChildren().add(btnDucks);
        root.getChildren().add(btnSharks);
        root.getChildren().add(btnBlues);
        Scene scene = new Scene(root, 960, 130);
        primaryStage.setTitle("HockeyButtons");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我找不到如何用图片
替换单词按钮 恩吉亚阳

如果您只想在按钮上显示图像而不是文本,那么您应该创建没有文本的按钮。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Project4 extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception {
        HBox root = new HBox();
        Button btnKings;
        Button btnDucks;
        Button btnSharks;
        Button btnBlues;
        btnKings = new Button();
        btnDucks = new Button();
        btnSharks = new Button();
        btnBlues = new Button();
        Image imgKings = new Image("kings.jpg");
        btnKings.setGraphic(new ImageView(imgKings));
        Image imgDucks = new Image("ducks.gif");
        btnDucks.setGraphic(new ImageView(imgDucks));
        Image imgSharks = new Image("sharks.gif");
        btnSharks.setGraphic(new ImageView(imgSharks));
        Image imgBlues = new Image("blues.gif");
        btnBlues.setGraphic(new ImageView(imgBlues));
        root.getChildren().add(btnKings);
        root.getChildren().add(btnDucks);
        root.getChildren().add(btnSharks);
        root.getChildren().add(btnBlues);
        Scene scene = new Scene(root, 960, 130);
        primaryStage.setTitle("HockeyButtons");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

在这里,您错误地显示了图像的路径。

Image imgKings = new Image("file:kings.jpg");

您最好显示绝对路径,或者如果图像位于类路径文件夹中,则通过:

String absolutePathToIcon =
                getClass().getResource("kings.jpg").toExternalForm();
Image imgKings = new Image(absolutePathToIcon);

相关内容

最新更新