当我有一个包含本地图像网址的列表时,如何显示它们?
这是我的代码,files
是我的列表,其中包含图片的地址(本地):
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
FileChooser fc = new FileChooser();
List<File> files = fc.showOpenMultipleDialog(primaryStage);
for(File file : files) {
System.out.println(file);
}
} catch(Exception e) {
e.printStackTrace();
}
}
所以基本上“文件”是我的图片的网址(本地),我想要点播它们
答案 0 :(得分:1)
你需要做类似的事情:
for(String s : ListOfURL){
File f = new File(s) ;
Image image = ImageIO.read(f) ;
}
答案 1 :(得分:0)
这是一个完全正常工作的应用程序,可以按照您的意愿使用它!
import java.awt.Dimension;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class NewClass {
public static void main(String[] args) {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.showOpenDialog(fc);
File[] ListOfURL = fc.getSelectedFiles();
JFrame frame = new JFrame();
JPanel imagePanel = new JPanel();
//ScrollPane to scroll though images
JScrollPane scroller = new JScrollPane(imagePanel);
imagePanel.setLayout(new BoxLayout(imagePanel, BoxLayout.PAGE_AXIS));
//For each image URL in the list
for (File file : ListOfURL) {
try {
//Get image from file
Image image = ImageIO.read(file);
//Create and add image label
JLabel label = new JLabel(new ImageIcon(image));
imagePanel.add(label);
} catch (IOException e) {
e.printStackTrace();
}
}
//Set and show frame
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(scroller);
frame.setSize(300, 300);
frame.setMaximumSize(new Dimension(600, 800));
frame.pack();
frame.setVisible(true);
}
}