怎么用库或者相关的类和方法:类JFileChooser 方法showOpenDialog()类JColorChooser
设计一个图形浏览程序!
来个简单的,功能还不错:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.beans.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileView;public class FileChooserTest
{
public static void main(String[] args)
{
ImageViewerFrame frame = new ImageViewerFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class ImageViewerFrame extends JFrame
{
public ImageViewerFrame()
{
setTitle(\"FileChooserTest\");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);JMenu menu = new JMenu(\"File\");
menuBar.add(menu);JMenuItem openItem = new JMenuItem(\"Open\");
menu.add(openItem);
openItem.addActionListener(new FileOpenListener());JMenuItem exitItem = new JMenuItem(\"Exit\");
menu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});label = new JLabel();
add(label);chooser = new JFileChooser();
// accept all image files ending with .jpg, .jpeg, .gif
final ExtensionFileFilter filter = new ExtensionFileFilter();
filter.addExtension(\"jpg\");
filter.addExtension(\"jpeg\");
filter.addExtension(\"gif\");
filter.setDescription(\"Image files\");
chooser.setFileFilter(filter);chooser.setAccessory(new ImagePreviewer(chooser));
chooser.setFileView(new FileIconView(filter,null));
}
private class FileOpenListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
chooser.setCurrentDirectory(new File(\".\"));int result = chooser.showOpenDialog(ImageViewerFrame.this);
if(result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
label.setIcon(new ImageIcon(name));
}
}
}public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 400;private JLabel label;
private JFileChooser chooser;
}
class ExtensionFileFilter extends FileFilter
{public void addExtension(String extension)
{
if (!extension.startsWith(\".\"))
extension = \".\" + extension;
extensions.add(extension.toLowerCase());
}
public void setDescription(String aDescription)
{
description = aDescription;
}
public String getDescription()
{
return description;
}public boolean accept(File f)
{
if (f.isDirectory()) return true;
String name = f.getName().toLowerCase();for (String extension : extensions)
if (name.endsWith(extension))
return true;
return false;
}
private String description = \"\";
private ArrayList<String> extensions = new ArrayList<String>();
}
class FileIconView extends FileView
{
public FileIconView(FileFilter aFilter, Icon anIcon)
{
filter = aFilter;
icon = anIcon;
}public Icon getIcon(File f)
{
if (!f.isDirectory() && filter.accept(f))
return icon;
else return null;
}
private FileFilter filter;
private Icon icon;
}
class ImagePreviewer extends JLabel
{public ImagePreviewer(JFileChooser chooser)
{
setPreferredSize(new Dimension(100, 100));
setBorder(BorderFactory.createEtchedBorder());chooser.addPropertyChangeListener(new
PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent
event)
{
if (event.getPropertyName() ==
JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)
{
File f = (File) event.getNewValue();
if (f == null) { setIcon(null); return; }
ImageIcon icon = new ImageIcon(f.getPath());if(icon.getIconWidth() > getWidth())
icon = new ImageIcon(icon.getImage().getScaledInstance(
getWidth(), -1, Image.SCALE_DEFAULT));setIcon(icon);
}
}
});
}
}