package Swing;
/*
* 功能:根据路径查看文件夹中的图片
* 作者:sun
* 时间:2011.4.1
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class See {
private JFrame frame;
private JButton seebutton,nextbutton,previousbutton,renovatebutton;
private JPanel northpanel,southpanel,panel;
private JTextField text;
private JLabel label;
private Icon icon;
private File[] fileArray;
int count;
int size;
public See(){
frame = new JFrame("Picture See Tool Author:sun E-Mail:sun_669@163.com");
seebutton = new JButton("Open Up");
nextbutton = new JButton(" Next ");
previousbutton = new JButton("Previous");
renovatebutton = new JButton("Repaint");
northpanel = new JPanel();
southpanel = new JPanel();
panel = new JPanel();
text = new JTextField(25);
label = new JLabel();
northpanel.add(text);
northpanel.add(seebutton);
northpanel.add(renovatebutton);
southpanel.setLayout(new GridLayout(1,2));
southpanel.add(previousbutton);
southpanel.add(nextbutton);
panel.add(label);
frame.add(northpanel,BorderLayout.NORTH);
frame.add(panel);
frame.add(southpanel,BorderLayout.SOUTH);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(d.width, d.height - 50);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
label.setForeground(Color.blue);
label.setFont(new Font("Dialog", Font.BOLD, 18));
MyActionListener mal = new MyActionListener();
seebutton.addActionListener(mal);
previousbutton.addActionListener(mal);
nextbutton.addActionListener(mal);
renovatebutton.addActionListener(mal);
nextbutton.setEnabled(false);
previousbutton.setEnabled(false);
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(seebutton)){
if(text.getText().equals("")){
JOptionPane.showMessageDialog(null,"提示:目录或路径不能为空");
}else{
label.setText("");
try{
String path = text.getText();
File file = new File(path);
if(file.isFile()){
label.setIcon(new ImageIcon(path));
}else{
fileArray = file.listFiles();
size = fileArray.length;
icon = new ImageIcon(fileArray[count].getAbsolutePath());
label.setIcon(icon);
if(size > 1){
nextbutton.setEnabled(true);
}
}
}catch (Exception ex) {
label.setForeground(Color.RED);
// lable.setFont(new Font("Dialog", Font.BOLD, 24));
JOptionPane.showMessageDialog(null,"提示:输入的文件路径或目录找不到");
}
}
return;
}
if(e.getSource().equals(nextbutton)){
if(count < size){
count++;
icon = new ImageIcon(fileArray[count].getAbsolutePath());
label.setIcon(icon);
previousbutton.setEnabled(true);
if(count == size -1){
nextbutton.setEnabled(false);
}
}
}
if(e.getSource().equals(previousbutton)){
if(count > 0){
count--;
icon = new ImageIcon(fileArray[count].getAbsolutePath());
label.setIcon(icon);
nextbutton.setEnabled(true);
}else if(count == 0){
previousbutton.setEnabled(false);
}
}
if(e.getSource().equals(renovatebutton)){
if(text.getText().equals("")){
JOptionPane.showMessageDialog(null,"您现在不必刷新<清除记录>");
}else{
label.setIcon(null);
label.setText(null);
text.setText(null);
count = 0;
size = 0;
nextbutton.setEnabled(false);
previousbutton.setEnabled(false);
JOptionPane.showMessageDialog(null,"刷新<清除>成功");
}
}
}
}
public static void main(String[] args) {
new See();
}
}