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.event.MouseListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.*;
import javax.swing.*;publicclassJFileFilterTest extends JFrame{publicJFileFilterTest() {
super();
setTitle("表格");
setBounds(100,100,350,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button=newJButton();
final JLabel label=newJLabel("双击选择照片",SwingConstants.CENTER);
label.addMouseListener(newMouseAdapter(){//适配器publicvoidmouseClicked(MouseEvent e){if(e.getClickCount()==2){//双击事件JFileChooser fileChooser=newJFileChooser();
FileFilter filter=newFileFilter() {//要过滤的文件publicboolean accept(File f) {//显示的文件类型if(f.isDirectory()) {returntrue;
}//显示满足条件的文件returnf.getName().endsWith(".jpg")||f.getName().endsWith(".gif");
}/**
* 这就是显示在打开框中*/publicString getDescription() {return"*.jpg,*.gif";
}
};//jdk1.6 FileNameExtensionFilter//new FileNameExtensionFilter("图像文件(jpg/gif)","jpg","jpeg","gif");fileChooser.setFileFilter(filter);inti=fileChooser.showOpenDialog(getContentPane());//opendialogif(i==JFileChooser.APPROVE_OPTION)//判断是否为打开的按钮{
File selectedFile=fileChooser.getSelectedFile();//取得选中的文件//label.setText(selectedFile.getPath());//取得路径label.setIcon(newImageIcon(selectedFile.getAbsolutePath()));//将图片显示在标签上}
}
}
});
getContentPane().add(button,BorderLayout.NORTH);//布局处理getContentPane().add(label,BorderLayout.CENTER);
button.setText("上传");
}/**
* @param args*/publicstaticvoidmain(String[] args) {//TODO Auto-generated method stubJFileFilterTest jFileFilterTest=newJFileFilterTest();
jFileFilterTest.setVisible(true);
}
}