Java 为什么不显示我的按钮图像设置图形方法?
我的bin文件夹中有图像文件,但当我运行我的程序时,我所拥有的只是没有被图像替换的按钮选项。我认为这与文件有关:在图像前面,但我不确定。这些图像实际上不是一个移动的gif(不知道为什么它被称为gif) 我找不到如何用图片替换单词按钮Java 为什么不显示我的按钮图像设置图形方法?,java,javafx,Java,Javafx,我的bin文件夹中有图像文件,但当我运行我的程序时,我所拥有的只是没有被图像替换的按钮选项。我认为这与文件有关:在图像前面,但我不确定。这些图像实际上不是一个移动的gif(不知道为什么它被称为gif) 我找不到如何用图片替换单词按钮 Nghia Duong 如果只想在按钮上显示图像而不显示文本,则应创建不带文本的按钮 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.cont
Nghia Duong 如果只想在按钮上显示图像而不显示文本,则应创建不带文本的按钮
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");
您最好通过以下方式显示绝对路径,或者如果图像位于classpath文件夹中:
String absolutePathToIcon =
getClass().getResource("kings.jpg").toExternalForm();
Image imgKings = new Image(absolutePathToIcon);
我找不到如何用图片替换单词按钮Nghia Duong 如果只想在按钮上显示图像而不显示文本,则应创建不带文本的按钮
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");
您最好通过以下方式显示绝对路径,或者如果图像位于classpath文件夹中:
String absolutePathToIcon =
getClass().getResource("kings.jpg").toExternalForm();
Image imgKings = new Image(absolutePathToIcon);
是否显示图像?我在我的案例中尝试了你的代码,它们根本没有显示。不,这就是问题所在。我找不到如何用图片替换文字按钮显示的是图片吗?我在我的案例中尝试了你的代码,它们根本没有显示。不,这就是问题所在。我找不到如何用图片替换单词按钮