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() {
@Override public void handle(ActionEvent e) {
Button button = (Button) e.getSource();
button.setGraphic(new ImageView(image));
}
});