使用FileChooser在Java中加载图像的问题 [英] Problem with loading image in java with FileChooser

查看:159
本文介绍了使用FileChooser在Java中加载图像的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个可以选择文件的类文件选择器.我已经搜索了一个加载图像但没有加载任何内容的类.我的filechooser类可以使面板打开并保存按钮.当我按下打开"按钮时,我可以搜索我的文档,当我要打开图像时,我的图像加载类没有响应,我需要一些可以与我同步的类,以便选择文件并从我的文档中加载我的图像并将其显示在面板中

I have write a class filechooser where I can choose files. I had searched for a class that loads images but nothing. My filechooser class can made a panel with button open and save. When I press button open I can search in my documents and when I'm going to open the image my image load class not responding I need some class that can synchronize with me filechooser and load my image from my documents and show it in the panel.

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);
}

}  

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

this is the class file chooser i need a class that can load the picture i open

这是图像加载代码.

  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.aboslutepath并在单击它时加载图片

what i want is the image code read the file.aboslutepath and load the picture when i click it

推荐答案

选择的所有文件用于获取要加载的文件的名称.您仍然需要加载文件.

All the file choose is used for is to get the name of the file you want to load. You still need to load the file.

首先阅读如何使用图标的Swing教程中的部分. 例如有关如何读取图像的代码.

Start by reading the section from the Swing tutorial on How to Use Icons for example code on how to read the image.

这篇关于使用FileChooser在Java中加载图像的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆