JAVA swing 根据路径查看图片

 

 

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();

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值