如何在javaFX中更改按钮的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31496079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:10:06 来源:igfitidea点击:
How to change image of button in javaFX?
提问by Eng.sabbath
I am using javaFX. I made a button and set an image for this . the code is :
我正在使用 javaFX。我制作了一个按钮并为此设置了一个图像。代码是:
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 ?
但是我想当我单击按钮时,图像会更改为另一张图片。我怎样才能做到这一点 ?
采纳答案by Reimeus
You could set the buttons Graphic in an Action
您可以在 Action 中设置按钮 Graphic
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));
}
});