您的位置:首页 > 其它

JFileChooser ,让你的图片选择可以预览图片内容

2009-02-06 20:22 260 查看
 


 

下面是很久前写的了,移到CSDN,希望对大家有帮助。

------------------------------------------------------------------------------------------2008-02-29

最近学习Swing,写了个文件选种器,可以浏览图片哦。很粗糙,不过很有意思。
首先是一个JPanel的继承类。用来设置文件选择器JFileChooser 1package book.swing.jfilechooser;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class ImageAccessory extends JPanel implements PropertyChangeListener{

private Icon icon;
private JLabel imageName;
private JLabel imageLabel;
private JScrollPane scrollpane;
public ImageAccessory(){
setLayout(new BorderLayout());
icon = new ImageIcon("/home/madic/adapter.png");
imageLabel = new JLabel(icon);
//    imagePanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
imageLabel.setBorder(BorderFactory.createLineBorder(Color.green));
scrollpane = new JScrollPane(imageLabel);
add(imageName = new JLabel("imagename"), BorderLayout.NORTH);
add(scrollpane, BorderLayout.CENTER);
}
public void propertyChange(PropertyChangeEvent pe){
if(pe.getPropertyName().equals(
JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)){
File f = (File)pe.getNewValue();
if(f==null){
return;
}
if(!f.isDirectory()){
System.out.println(f.getAbsolutePath());
imageName.setText(f.getName());
ImageIcon ii = new ImageIcon(f.getAbsolutePath());
Image image = ii.getImage().getScaledInstance(200, 300, Image.SCALE_DEFAULT);
ii.setImage(image);
icon = ii;
imageLabel.setIcon(icon);
imageLabel.updateUI();
this.updateUI();
}
}
}
}


 

下面就是一个演示JFileChooser的例子了:

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ImageFileChooser extends JFrame{
private JButton open;
private JFrame parent;
private final JLabel statusbar;
public ImageFileChooser(){
super("ImageFileChooser");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
parent = this;
statusbar = new JLabel(
"Output of your selection will go here");
Container c = getContentPane();
c.setLayout(new FlowLayout());
open = new JButton("Open");
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
ImageAccessory ia = new ImageAccessory();
chooser.setAccessory(ia);
chooser.addPropertyChangeListener(ia);
int option = chooser.showOpenDialog(parent);
if(option == JFileChooser.APPROVE_OPTION){
statusbar.setText("You chose "
+ ((chooser.getSelectedFile() != null) ? chooser
.getSelectedFile().getName() : "nothing"));
} else {
statusbar.setText("You canceled.");
}
}
});

c.add(open);
c.add(statusbar);
setSize(200,300);
}

public static void main(String[] args){
new ImageFileChooser().setVisible(true);
}
}


 

关键在于 需要设置JFileChooser的 accessory属性,我们在这个JPanel中来实现我们的东西。还要注意的是我们的JPanel类需要实现一个PropertyChangeListener类,这样,当你选择改变的时候,就可以在函数
propertyChange(PropertyChangeEvent ev)中实现动作了。
其中注意getScaledInstance(200, 300, Image.SCALE_DEFAULT);这个函数主要用来设置图片的大小。最后一个参数:Image.SCALE_DEFAULT是改变图像大小的算法。
要点就这么多。呵呵。试试看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐