package cn.lit.edu;import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;public class ImageType extends JFrame implements FilenameFilter { private JPanel contentPane;
private JLabel label_image;
private JButton btn_chooseDir;
private JFileChooser fileChooser;
private JButton btn_pre;
private JButton btn_next;
private JToggleButton tBtn_startOrPause;
private String[] files;
private String chooseName;
private int imageIndex; /**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ImageType frame = new ImageType();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
 * Create the frame.
 */
public ImageType() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setBounds(100, 100, 760, 660);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getLabel_image());
contentPane.add(getBtn_chooseDir());
contentPane.add(getBtn_pre());
contentPane.add(getBtn_next());
contentPane.add(getTBtn_startOrPause());
} private JLabel getLabel_image() {
if (label_image == null) {
label_image = new JLabel("");
label_image.setIcon(null);
label_image.setBounds(12, 10, 735, 560);
}
return label_image;
} private JButton getBtn_chooseDir() {
if (btn_chooseDir == null) {
btn_chooseDir = new JButton("选择目录");
btn_chooseDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = getFileChooser().showOpenDialog(
ImageType.this);
if (returnVal == JFileChooser.CANCEL_OPTION) {
return;
}
chooseName = fileChooser.getSelectedFile()
.getAbsolutePath();
System.out.println(chooseName);
File file = new File(chooseName);
if (file.isFile()) {
label_image.setIcon(new ImageIcon(chooseName));//这里也能够显示
chooseName = file.getParentFile().getAbsolutePath();
files = file.list(ImageType.this);
imageIndex = 0;
return;
}
files = file.list(ImageType.this);
imageIndex = 0;
}
});
btn_chooseDir.setBounds(623, 589, 95, 25);
}
return btn_chooseDir;
} public JFileChooser getFileChooser() {
if (fileChooser == null) {
fileChooser = new JFileChooser("C:" + File.separator
+ "Documents and Settings" + File.separator + "jsjx"
+ File.separator + "My Documents" + File.separator
+ "My Pictures");
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setDialogTitle("选择图片目录");
fileChooser.setFileFilter(new FileNameExtensionFilter(
"JPG & GIF & PNG", "jpg", "jpeg", "gif", "png"));
fileChooser.setAcceptAllFileFilterUsed(false);
}
return fileChooser;
} private JButton getBtn_pre() {
if (btn_pre == null) {
btn_pre = new JButton("<--");
btn_pre.setBounds(22, 589, 60, 25);
btn_pre.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (files != null && files.length != 0 && imageIndex != 0) {
System.out.println(chooseName+File.separator+files[imageIndex]);
imageIndex--;
String imagePath = chooseName+files[imageIndex];
label_image.setIcon(new ImageIcon(imagePath));
return;
}
JOptionPane.showConfirmDialog(ImageType.this, "这已经是第一张图片!",
"提醒", JOptionPane.OK_OPTION,
JOptionPane.WARNING_MESSAGE);
}
});
}
return btn_pre;
} private JButton getBtn_next() {
if (btn_next == null) {
btn_next = new JButton("-->");
btn_next.setBounds(202, 589, 60, 25);
btn_next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label_image.setIcon(new ImageIcon("/home/wlxxsj/Pictures/1.png"));//这里可以显示
if (files != null && files.length != 0 && imageIndex < files.length) {
System.out.println(chooseName+File.separator+files[imageIndex]);
imageIndex++;
String imagePath = chooseName+files[imageIndex];
label_image.setIcon(new ImageIcon(imagePath));//但这里就显示不出来
return;
}
JOptionPane.showConfirmDialog(ImageType.this, "这已经是最后一张图片!",
"提醒", JOptionPane.OK_OPTION,
JOptionPane.WARNING_MESSAGE);
}
});
}
return btn_next;
} private JToggleButton getTBtn_startOrPause() {
if (tBtn_startOrPause == null) {
tBtn_startOrPause = new JToggleButton("开始");
tBtn_startOrPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tBtn_startOrPause.getText().equals("开始")) {
tBtn_startOrPause.setText("暂停");
btn_next.setEnabled(false);
btn_pre.setEnabled(false);
return;
}
tBtn_startOrPause.setText("开始");
btn_next.setEnabled(true);
btn_pre.setEnabled(true);
}
});
tBtn_startOrPause.setBounds(116, 589, 52, 25);
}
return tBtn_startOrPause;
} @Override
public boolean accept(File dir, String name) {
Pattern p = Pattern
.compile("^.*?\\.([j,J][p,P][g,G]|[j,J][p,P][e,E][g,G]|[p,P][n,N][g,G]|[g,G][i,I][f,F])$");
Matcher m = p.matcher(name);
return m.matches();
}
}
以上在getBtn_chooseDir()的public void actionPerformed(ActionEvent e)中设置label_image.setIcon(new ImageIcon(chooseName))能够显示图片,但是在其他按钮的public void actionPerformed(ActionEvent e)中设置就不行。
纠结啊!