如何通过 JFileChooser 将 Icon 对象保存到文件中?

发布于 2024-12-19 09:55:36 字数 1900 浏览 0 评论 0原文

我有一个 JLabel,在其中保存了 ImageIcon,如下所示:

ImageIcon imageIcon = sample.map(); // a map method create an ImageIcon object
imageLabel.setIcon(imageIcon);
imageLabel.setVisible(true);

现在,我想在单击“保存”项菜单时将此 ImageIcon 对象保存到 PNG 文件中。

private void imageActionPerformed(java.awt.event.ActionEvent evt) {

    Icon pic = imageLabel.getIcon();

    JFileChooser fileChooser = new JFileChooser("C:/");  

    fileChooser.setSelectedFile(file);    

    // this filter will allow just PNG extension  
    FileFilter filter = new MyCustomFilter2();
    fileChooser.setFileFilter(filter);

    int returnVal = fileChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
        File fileToSave = fileChooser.getSelectedFile();
     }
    else
    {
    System.out.println("File access cancelled by user.");
    }
}

是的,我知道这段代码是错误的,并且缺少某些部分,我想我应该以某种方式将名为 pic 的 Icon 对象保存到 File 对象中。这是我的假设。请问我该怎么做?

感谢您的帮助,

迈克尔。

这是我的源代码

private void imageActionPerformed(java.awt.event.ActionEvent evt) {
    try{
    Icon image = imageLabel.getIcon();

    BufferedImage bi = new BufferedImage(image.getIconWidth(),image.getIconHeight(),BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();

    File file = new File("outputFile");
    JFileChooser fileChooser = new JFileChooser("C:/");  
    fileChooser.setSelectedFile(file);    

    FileFilter filter = new MyCustomFilter2();
    fileChooser.setFileFilter(filter);

    int returnVal = fileChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
     {
      ImageIO.write(bi, "PNG", file);
      File fileToSave = fileChooser.getSelectedFile();

     }
    else
     {
      System.out.println("File access cancelled by user.");
     }
  }

catch(IOException e){
    e.printStackTrace();
   }
}

I have a JLabel inside which I have saved my ImageIcon like this:

ImageIcon imageIcon = sample.map(); // a map method create an ImageIcon object
imageLabel.setIcon(imageIcon);
imageLabel.setVisible(true);

Now I would like to save this ImageIcon object into a PNG file when clicking on the Save item menu.

private void imageActionPerformed(java.awt.event.ActionEvent evt) {

    Icon pic = imageLabel.getIcon();

    JFileChooser fileChooser = new JFileChooser("C:/");  

    fileChooser.setSelectedFile(file);    

    // this filter will allow just PNG extension  
    FileFilter filter = new MyCustomFilter2();
    fileChooser.setFileFilter(filter);

    int returnVal = fileChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    {
        File fileToSave = fileChooser.getSelectedFile();
     }
    else
    {
    System.out.println("File access cancelled by user.");
    }
}

Yes I know that this code is wrong and some part is missing, I think I should somehow save my Icon object called pic into a File object. This is my assumption. How can I do it please?

Thanks for any help,

Michal.

Here is my source code

private void imageActionPerformed(java.awt.event.ActionEvent evt) {
    try{
    Icon image = imageLabel.getIcon();

    BufferedImage bi = new BufferedImage(image.getIconWidth(),image.getIconHeight(),BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();

    File file = new File("outputFile");
    JFileChooser fileChooser = new JFileChooser("C:/");  
    fileChooser.setSelectedFile(file);    

    FileFilter filter = new MyCustomFilter2();
    fileChooser.setFileFilter(filter);

    int returnVal = fileChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION)
     {
      ImageIO.write(bi, "PNG", file);
      File fileToSave = fileChooser.getSelectedFile();

     }
    else
     {
      System.out.println("File access cancelled by user.");
     }
  }

catch(IOException e){
    e.printStackTrace();
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流心雨 2024-12-26 09:55:36

JFileChooser 返回的 File 对象仅表示用户想要保存文件的磁盘位置。之后,您将需要使用 ImageIO.write() 将文件保存到磁盘。

例如

ImageIO.write(image, "png", file);

,如果您有一个图标,我认为您可能需要将其转换为 BufferedImage,然后才能保存它。

The File object returned by the JFileChooser just represents the location on disk where the user would like to save the file. After that you'll want to use ImageIO.write() to save the file to disk.

e.g.

ImageIO.write(image, "png", file);

If you have an Icon, I think you may need to convert that to a BufferedImage before you can save it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文