<pre name="code" class="java">package com.hechao;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileFilter;
/**
* 图片浏览显示窗口
* @author hechao
*
*/
@SuppressWarnings("serial")
public class MyFrame extends JFrame{
private JButton scanButton;
private JFileChooser chooser;
private JLabel pictrueLable = null;
private String imageName = null;
public MyFrame(){
this.setTitle("图片显示窗口");
this.setSize(600, 700);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
pictrueLable = new JLabel("", JLabel.CENTER);
pictrueLable.setBounds(40, 40, 500, 550);
this.add(pictrueLable);
chooser = new JFileChooser();
chooser.setFileFilter(new FileFilter(){
@Override
public String getDescription() {
return "图片";
}
@Override
public boolean accept(File f) {
return true;
}
});
scanButton = new JButton("浏览");
scanButton.setBounds(270, 630, 60, 30);
scanButton.addActionListener(new ActionListener() {
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent e) {
int i = chooser.showOpenDialog(null);
if(i == chooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
imageName = f.getAbsolutePath();
}
pictrueLable.setIcon(new ImageIcon(imageName));
}
});
this.add(scanButton);
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}