【发布时间】:2017-03-07 12:01:11
【问题描述】:
我正在尝试在单击按钮时更改 ImageView 组件中的图像。我需要它是本地图像。我不断收到路径错误,无法使用 ImageIcon 并将其转换为 Image。有没有一种简单的方法可以做到这一点?
package tictactoesimulator_alliebeckman;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
/**
*
* @author Allie
*/
public class FXMLDocumentController implements Initializable {
// my components
@FXML
public Label lblWinLose;
@FXML
public Button btnNewGame;
@FXML
public ImageView ivOne;
// Event handle for button click
@FXML
public void handleButtonAction(ActionEvent event) {
// here is where I'm having the issue I have the image file in my src folder
// I've tried using a ImageIcon and it wont convert to an Image?
// All I need is the image to change to the local image on button click
lblWinLose.setText("Clicked");
Image image = new Image("o.png");
ivOne = new ImageView(image);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
项目布局可以在这个 Eclipse 截图中看到:
【问题讨论】:
-
请您发布堆栈跟踪
-
java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
标签: java image javafx imageview scenebuilder