我创建了一个 GUI,它使用以下类打开文件选择器:
public class FileSelector {
File fp;
BufferedImage selectedFile;
public void SelectFile() {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Select an image");
jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG and jpeg images", "png", "jpg", "jpeg");
jfc.addChoosableFileFilter(filter);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
fp = jfc.getSelectedFile();
System.out.println(fp);
}
}
public BufferedImage createBufferedImage() throws IOException {
ImageFileHandler img_handler = new ImageFileHandler();
if (fp.isFile() && fp.exists()) {
selectedFile = ImageIO.read(fp);
System.out.println(selectedFile);
}
BufferedImage bimage = new BufferedImage(28, 28, BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(selectedFile, 0, 0, null);
System.out.println(bimage);
bGr.dispose();
JFrame frame = new JFrame("Image from Desktop");
JLabel picLabel = new JLabel(new ImageIcon(bimage));
JPanel jPanel = new JPanel();
jPanel.add(picLabel);
frame.setSize(new Dimension(400, 300));
frame.add(jPanel);
frame.setVisible(true);
return bimage;
}
}
然后我有另一个类使用swing 来构建GUI。当前,所选图像在单独的 JFrame 中打开。我希望图像显示在主界面中displayPanel
慕神8447489
相关分类