如何使用JFileChooser将图像加载到JLabel

时间:2018-01-12 20:53:12

标签: java jfilechooser

这是我的包裹。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

我试图使用其他功能的JFileChooser将图像加载到JLabel中。我试图返回图像,因为我的文件选择器无法在JLabel中加载图像。

GraphologyAnalysis() here is declaration of my pane.
public GraphologyAnalysis()  
{
    super("Graphology Analysis");
    setSize(width,height);
    Container pane= getContentPane();
    pane.setBackground(Color.white); 
    pane.setLayout(null);

    displayImg= new JLabel (new ImageIcon ("How to return in an image in this?"));
    displayImg.setSize(580,310);
    displayImg.setLocation(480,150);
    pane.add(displayImg);

以下此功能来自JFileChooser。文件选择器以某种方式不加载图像。

private BufferedImage imgUp;
public void showfile() throws java.io.IOException 
{
    File("C:/Users/User/Pictures/Vita Wallpaper"));
    JFileChooser temp = new JFileChooser();
    temp.setCurrentDirectory(new File(System.getProperty("user.home")));
    int result = temp.showOpenDialog(this);

    File file = new File("C:/Users/User/Pictures");

    if (result == JFileChooser.APPROVE_OPTION)
    {
        file = temp.getSelectedFile();
        imgUp = ImageIO.read(file);
        displayImg=new JLabel (new ImageIcon (imgUp));
        displayImg.setSize(580,310);
        displayImg.setLocation(480,150);
        add(displayImg);
    }

0 个答案:

没有答案