在滚动窗格JavaFx中动态显示图像
我想通过单击下面的按钮在Scollpane中添加多个图像。我尝试了下面的代码,但它不会显示图像。对此有什么想法吗在滚动窗格JavaFx中动态显示图像,javafx,Javafx,我想通过单击下面的按钮在Scollpane中添加多个图像。我尝试了下面的代码,但它不会显示图像。对此有什么想法吗 @FXML private void OnClick(ActionEvent ae) { getGalleryView(); } public void getGalleryView() { ScrolPane sp=new ScroPane(); Hbox hb=new Hbox(); Image [] images=new Image[5]; ImageView []pi
@FXML private void OnClick(ActionEvent ae)
{
getGalleryView();
}
public void getGalleryView()
{
ScrolPane sp=new ScroPane();
Hbox hb=new Hbox();
Image [] images=new Image[5];
ImageView []pics=new ImageView[5];
final String [] imageNames = new String [] {"fw1.jpg", "fw2.jpg",
"fw3.jpg", "fw4.jpg", "fw5.jpg"};
for (int i = 0; i < 5; i++) {
images[i] = new Image(getClass().getResourceAsStream(imageNames[i]));
pics[i] = new ImageView(images[i]);
pics[i].setFitWidth(100);
pics[i].setPreserveRatio(true);
hb.getChildren().add(pics[i]);
sp.setContent(hb);
}
}
@FXML private void OnClick(ActionEvent ae)
{
getGalleryView();
}
public void getGalleryView()
{
ScrolPane sp=新的ScrolPane();
Hbox hb=新的Hbox();
图像[]图像=新图像[5];
ImageView[]pics=新建ImageView[5];
最终字符串[]imageNames=新字符串[]{“fw1.jpg”,“fw2.jpg”,
“fw3.jpg”、“fw4.jpg”、“fw5.jpg”};
对于(int i=0;i<5;i++){
images[i]=新图像(getClass().getResourceAsStream(imageNames[i]);
pics[i]=新图像视图(图像[i]);
pics[i].setFitWidth(100);
pics[i].设定保留率(真);
hb.getChildren().add(pics[i]);
sp.setContent(hb);
}
}
您需要将滚动窗格添加到场景中:
@FXML private void OnClick(ActionEvent ae)
{
getGalleryView(ae);
}
public void getGalleryView(ActionEvent ae)
{
ScrolPane sp=new ScroPane();
Hbox hb=new Hbox();
Image [] images=new Image[5];
ImageView []pics=new ImageView[5];
final String [] imageNames = new String [] {"fw1.jpg", "fw2.jpg",
"fw3.jpg", "fw4.jpg", "fw5.jpg"};
for (int i = 0; i < 5; i++) {
images[i] = new Image(getClass().getResourceAsStream(imageNames[i]));
pics[i] = new ImageView(images[i]);
pics[i].setFitWidth(100);
pics[i].setPreserveRatio(true);
hb.getChildren().add(pics[i]);
sp.setContent(hb);
}
Scene scene = ((Node) ae.getSource()).getScene();
((Pane) scene.getRoot()).getChildren().add(sp);
}
@FXML private void OnClick(ActionEvent ae)
{
getGalleryView(ae);
}
公共无效getGalleryView(ActionEvent ae)
{
ScrolPane sp=新的ScrolPane();
Hbox hb=新的Hbox();
图像[]图像=新图像[5];
ImageView[]pics=新ImageView[5];
最终字符串[]imageNames=新字符串[]{“fw1.jpg”,“fw2.jpg”,
“fw3.jpg”、“fw4.jpg”、“fw5.jpg”};
对于(int i=0;i<5;i++){
images[i]=新图像(getClass().getResourceAsStream(imageNames[i]);
pics[i]=新图像视图(图像[i]);
pics[i].setFitWidth(100);
pics[i].设定保留率(真);
hb.getChildren().add(pics[i]);
sp.setContent(hb);
}
场景场景=((节点)ae.getSource()).getScene();
((窗格)scene.getRoot()).getChildren().add(sp);
}
这里我假设您的根节点是一个窗格或其子类之一;错误
编辑:
我正在开发类似的方法。我的很好用。如果你愿意,你可以查一下
private List<String> listFileNames(File folder) throws NullPointerException{
List<String> list = new ArrayList<>();
for (File file : folder.listFiles()) {
if (file.isDirectory())
listFileNames(file);
else {
System.out.println(file.getName());
list.add(file.getName());
}
}
return list;
}
private void insertImages(List<String> list, Hero thisHero) {
int column = 0;
int row = 0;
for (String path:list) {
String fullPath = "file:"+thisHero.getHeroClass().getFile()+"\\"+path;
ToggleButton button = new ToggleButton();
button.setBackground(Background.EMPTY);
button.setGraphic(new ImageView(new Image(fullPath)));
grid.add(button,column,row);
column++;
if (column == 5) {
row++;
column = 0;
}
}
}
私有列表列表文件名(文件夹)引发NullPointerException{
列表=新的ArrayList();
对于(文件:folder.listFiles()){
if(file.isDirectory())
列表文件名(文件);
否则{
System.out.println(file.getName());
list.add(file.getName());
}
}
退货清单;
}
私有虚空插入图像(列表列表,英雄-本英雄){
int列=0;
int行=0;
用于(字符串路径:列表){
String fullPath=“file:“+thisHero.getHeroClass().getFile()+”\\“+路径;
ToggleButton=新的ToggleButton();
按钮.背景(背景.空);
设置图形(新图像视图(新图像(完整路径));
添加(按钮、列、行);
列++;
如果(列==5){
行++;
列=0;
}
}
}
如果你愿意,我可以写更多。我使用列表是因为它易于添加项目
您可以使用第一种方法,从充满图像文件的文件夹中获取要列出的所有文件名
第二种方法是用带有图形的切换按钮创建新的ImageView。我只是把概念改成了按钮,很抱歉我懒得修改代码来完全满足您的需要
Path是确切的文件名,thisHero.getHeroClass().getFile()
返回包含此映像的目录的路径
grid.add(按钮、列、行)
将此按钮添加到我之前制作的网格窗格中。这是我的应用程序,很抱歉没有分享所有代码,但我认为这段代码可能有用
EDIT2:如果有错误信息,您也可以向我们提供