怎么用库或者相关的类和方法:类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);
}
}
});
}
}
----------------解决方案--------------------------------------------------------
不错啊
----------------解决方案--------------------------------------------------------
一个程序的可读性要有注释啊!!!
不能这样回答别人啊
----------------解决方案--------------------------------------------------------
程序很简单,有不懂的地方可以自己查JAVA 文档.
----------------解决方案--------------------------------------------------------
哈哈,eastsun有我当年的风范!好!当年做版主,这么拽,得罪了不少人,和Live41,神飘飘,Knocker并称bc-cn四大恶人。。。其实也不是不愿意教别人,关键还是要培养大家自学的能力,要不学不到东西
----------------解决方案--------------------------------------------------------
...
让前辈见笑了.
我觉得JAVA文档上有的东西就应该自己去找啊,学会自己看JAVA文档是学习java的一个重要能力.
其实我自己也很菜啊,学习JAVA GUI编程不久,而且全是自学(我不是搞计算机的,学习java完全是爱好).
偶尔回下别人的贴也主要是想训练下自己的JAVA能力,不然平时很少用到,不经常用就容易忘记.
----------------解决方案--------------------------------------------------------
...
让前辈见笑了.
我觉得JAVA文档上有的东西就应该自己去找啊,学会自己看JAVA文档是学习java的一个重要能力.
其实我自己也很菜啊,学习JAVA GUI编程不久,而且全是自学(我不是搞计算机的,学习java完全是爱好).
偶尔回下别人的贴也主要是想训练下自己的JAVA能力,不然平时很少用到,不经常用就容易忘记.
你JAVA GUI学得很好哦
不错.
----------------解决方案--------------------------------------------------------
斑竹过奖了.
我是东学一点,西学一点,觉得什么有意思就学什么.
无论是基本功还是编程水平都比你们差远了.
----------------解决方案--------------------------------------------------------
Eastsun怎么实现图片翻转,放大啊
----------------解决方案--------------------------------------------------------