我正在开发Java FX应用程序,并且我想在运行时在视图中根据输入数字显示多个图像。我尝试在我的控制器类中执行此操作,但它没有显示图像。
private ImageView[] raccogliImg(){
File file = new File("application/fotovoltaico1.jpg");
for(int i=0; i<v.length; i++) {
v[i] = new ImageView(new Image(file.toURI().toString()));
anchor.getChildren().add(v[i]);
anchor.requestLayout();
}
return v;
}
@FXML
void onClick(ActionEvent event) {
raccogliImg();
}
这是控制器类代码
public class Controller {
public ImageView[] v = new ImageView[6];
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private Button button;
@FXML
private Pane pane;
private ImageView[] raccogliImg(){
String file = "application/fotovoltaico1.jpg";
Image img = new Image(file);
for(int i=0; i<v.length; i++) {
v[i] = new ImageView(img);
pane.getChildren().add(v[i]);
pane.requestLayout();
}
return v;
}
@FXML
void onClick(ActionEvent event) {
raccogliImg();
}
@FXML
void initialize() {
assert button != null : "fx:id=\"button\" was not injected: check your FXML file 'prova.fxml'.";
}
}
答案 0 :(得分:0)
您的代码无法正确获取图片。
String file = "application/fotovoltaico1.jpg";
Image image = new Image(file);
ImageView iv = new ImageView(image);
这将有效。
答案 1 :(得分:0)
我只是复制粘贴你的代码并修复为可编译和可运行。使用VBox代替Pane,使图像不会重叠。它没有问题:
public class JavaFXApplication extends Application
{
public ImageView[] v = new ImageView[6];
private VBox pane = new VBox();
@Override
public void start( Stage stage )
{
raccogliImg();
stage.setScene( new Scene( pane ) );
stage.show();
}
private ImageView[] raccogliImg()
{
String file = "javafxapplication/applelogo.png";
Image img = new Image( file );
for ( int i = 0; i < v.length; i++ )
{
v[i] = new ImageView( img );
pane.getChildren().add( v[i] );
pane.requestLayout();
}
return v;
}
public static void main( String[] args )
{
Application.launch( JavaFXApplication.class, args );
}
}
所以你的问题出在不同的地方。