JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
jButton1.addActionListener(this);
if(e.getSource()==jButton1)
{
int i = chooser.showOpenDialog(jPanel1);
if(i==JFileChooser.APPROVE_OPTION)
{
jPanel2.removeAll();
jPanel2.repaint();
File image = chooser.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
//jPanel2.add(new JLabel(photo));
JLabel label=new JLabel("",photo,JLabel.CENTER);
jPanel2.add(label,BorderLayout.CENTER);
jPanel2.repaint();// sets a default image in image field.
jPanel2.revalidate();
}
}
注有很好的指導/教程:你應該爲jpanel2 設置borderlayout,並且所選圖像大小必須是jpanel2的大小