java - 使用FileChooser在java中加载图像的问题

标签 java image filechooser

我编写了一个类文件选择器,我可以在其中选择文件。我搜索了一个加载图像的类,但什么也没有。我的文件选择器类可以制作一个带有打开和保存按钮的面板。当我按下打开按钮时,我可以在文档中搜索,当我要打开图像时,我的图像加载类没有响应,我需要一些可以与我的文件选择器同步并从文档中加载图像并将其显示在面板中的类。

package project;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;

public class FileChooserDemo extends JPanel
                         implements ActionListener {
    static private final String newline = "\n";
    JButton openButton, saveButton;
    JTextArea log;
    JFileChooser fc;

public FileChooserDemo() {
    super(new BorderLayout());

    //Create the log first, because the action listeners
    //need to refer to it.
    log = new JTextArea(5,20);
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);

    //Create a file chooser
    fc = new JFileChooser();


    openButton = new JButton("Open a File...");

    openButton.addActionListener(this);

    //Create the save button.  We use the image from the JLF
    //Graphics Repository (but we extracted it from the jar).
    saveButton = new JButton("Save a File...");

    saveButton.addActionListener(this);

    //For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel(); //use FlowLayout
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton);

    //Add the buttons and the log to this panel.
    add(buttonPanel, BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e) {

    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);


        } else {
            log.append("Open command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());

    //Handle save button action.
    } else if (e.getSource() == saveButton) {
        int returnVal = fc.showSaveDialog(FileChooserDemo.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would save the file.
            log.append("Saving: " + file.getName() + "." + newline);
        } else {
            log.append("Save command cancelled by user." + newline);
        }
        log.setCaretPosition(log.getDocument().getLength());
    }
}



/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
public static void createLoad() {
    //Create and set up the window.
    JFrame frame = new JFrame("FileChooserDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add content to the window.
    frame.add(new FileChooserDemo());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

}  

这是类文件选择器,我需要一个可以加载我打开的图片的类

这是图像加载代码。

  import java.awt.*;
  import java.awt.event.*;
  import java.awt.image.*;
  import java.io.*;
  import javax.imageio.*;
  import javax.swing.*;

/**
 * This class demonstrates how to load an Image from an external file
*/
public class LoadImageApp extends Component {

   BufferedImage img;

public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}

public LoadImageApp() {
   try {
       img = ImageIO.read(new File("strawberry.jpg"));
   } catch (IOException e) {
   }

}

public Dimension getPreferredSize() {
    if (img == null) {
         return new Dimension(100,100);
    } else {
       return new Dimension(img.getWidth(null), img.getHeight(null));
   }
}

public static void main(String[] args) {

    JFrame f = new JFrame("Load Image Sample");

    f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    f.add(new LoadImageApp());
    f.pack();
    f.setVisible(true);
}
}

我想要的是图像代码读取file.abosolutepath并在我点击它时加载图片

最佳答案

要获取选定的文件,您应该在 JFileChooser 停止后使用 getSelectedFile。

显示它:

您还可以使用在其上渲染图像的组件。

这个是我制作的,位于我的项目下,是一个 JPanel 扩展,可让您在其上添加组件(在图像上)

https://github.com/MarkyVasconcelos/Towel/wiki/JImagePanel

关于java - 使用FileChooser在java中加载图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5638988/

相关文章:

java - 在 JavaFX 中从 FileChooser 打开图像

java - 尝试查看用户通过使用数组列表填写了多少编辑文本

java - 将映射映射到包含名称和值参数的对象列表

javascript - 单图不同背景

jquery - 制作一个带背景的按钮

events - JavaFX - 为什么我的 FileChooser 允许我访问原始舞台?

image - 使用 FileChooser 保存可写图像图像

java - 我如何在 Java 中将对象转换为 int?

java - 我如何在Android中实现 fragment 的通用ArrayList?

android - 在 Image View Android 中获取模糊图像