每次从文件夹加载图像时,JPanel都不会刷新

时间:2015-06-09 15:57:06

标签: jpanel

我遇到了问题。我从文件夹加载图像,它可以在JPanel中第一次显示。但是当我尝试其他时候,JPanel并没有刷新。它始终保留第一张图像。非常感谢任何帮助!谢谢!

package com.user_GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

import javax.media.jai.JAI;
import javax.media.jai.RenderedOp;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.*;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGEncodeParam;

/* ResizableComponent.java */

public class TrySomething extends JFrame implements ActionListener {

private JPanel panel = new JPanel(null);
private Resizable resizer;

BufferedImage image = null;
JMenuBar menubar;
JMenu menu;
JMenuItem jitem;
String filePath = null;
String imageName = null;
String parentPath = null;

public TrySomething() throws Exception {

    setLayout(new BorderLayout());

    add(panel, BorderLayout.CENTER);

    menubar = new JMenuBar();
    menu = new JMenu("File");
    jitem = new JMenuItem("Open File...");

    // setLayout(new FlowLayout());
    //this.setJMenuBar(menubar);
    this.add(menubar, BorderLayout.NORTH);

    menubar.add(menu);
    menu.add(jitem);

    jitem.addActionListener(this);

    JPanel area = new JPanel();
    // area.setBackground(getBackground());
    // area.setBackground(null);
    area.setOpaque(false);
    resizer = new Resizable(area);
    resizer.setBounds(50, 50, 200, 150);
    //panel.setOpaque(false);//very important!
    panel.add(resizer);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(350, 300));
    setTitle("Resizable Component");
    setLocationRelativeTo(null);

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent me) {

            requestFocus();
            resizer.repaint();
        }
    });
}

public static void main(String[] args) throws Exception {
    TrySomething ts = new TrySomething();
    ts.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    JFileChooser fileChooser = new JFileChooser();

    int status = fileChooser.showOpenDialog(null);

    if (status == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();

        System.out.println(selectedFile.getPath());
        System.out.println(selectedFile.getName());
        System.out.println(selectedFile.getParent());
        filePath = selectedFile.getPath();
        imageName = selectedFile.getName();
        parentPath = selectedFile.getParent();

        try {
            showImage(filePath, imageName, parentPath);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    } else if (status == JFileChooser.CANCEL_OPTION) {
        System.out.println("calceled");

    }

}

public void showImage(String filePath, String imageName, String parentPath)
        throws Exception {
    String[] strings = imageName.split("\\.");

    String input2 = filePath;
    String output2 = parentPath + "/" + strings[0] + ".jpg";

    RenderedOp src2 = JAI.create("fileload", input2);
    OutputStream os2 = null;
    os2 = new FileOutputStream(output2);
    JPEGEncodeParam param2 = new JPEGEncodeParam();
    ImageEncoder enc2 = ImageCodec.createImageEncoder("JPEG", os2, param2);
    enc2.encode(src2);
    os2.close();



//      JPanel imagePanel = (JPanel) this.getContentPane();
//      imagePanel.setOpaque(false);
    JLabel label = new JLabel();

    ImageIcon icon = new ImageIcon(output2);

    System.out.println(output2);

    label.setIcon(icon);
    label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());

    System.out.println(label.getIcon().getIconHeight());


    //panel.updateUI();

    Thread.sleep(1000);
    //this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
    panel.add(label);

    Thread.sleep(1000);
    panel.repaint();
    panel.revalidate();

}

}

0 个答案:

没有答案