如何在javaFX中更改按钮的图像? [英] How to change image of button in javaFX?
本文介绍了如何在javaFX中更改按钮的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用javaFX。
我做了一个按钮并为此设置了图像。代码是:
I am using javaFX. I made a button and set an image for this . the code is :
Image playI=new Image("file:///c:/Users/Farhad/Desktop/icons/play2.jpg");
ImageView iv1=new ImageView(playI);
iv1.setFitHeight(67);
iv1.setFitWidth(69);
Button playB=new Button("",iv1);
但我希望当我点击按钮时,图像会变为另一张图片。
我该怎么做?
But i want to when i click the button , image changes to another picture. How can i do this ?
推荐答案
你可以在一个动作中设置按钮图形
You could set the buttons Graphic in an Action
Image image = new Image(getClass().getResourceAsStream("play3.jpg"));
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
Button button = (Button) e.getSource();
button.setGraphic(new ImageView(image));
}
});
这篇关于如何在javaFX中更改按钮的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文