照片浏览器

       昨天写了个图片浏览器,很简单,主要就是用一个JFileChooser导入一个文件,然后在文件父目录下导入下一个 或者上一个图像。 东西做好了,但是还有一个问题,就是我界面图片的路径设置有问题,在打成Jar包后就不能正常显示我的UI图片了。留着以后再解决吧。

 

package ImageScanner;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

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.filechooser.FileFilter;

public class ImageScanner extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new ImageScanner();
	}

	private JLabel ImageLabel;
	private JPanel mainPanel;
	private JPanel btnPanel;
	private JButton PrevImagebtn;
	private JButton InsertImagebtn;
	private JButton NextImagebtn;
	private JFileChooser fc = new JFileChooser();;
	private File file = null;
	private File[] parentfiles;
	private int ImageIndex = 0; 
	ImageIcon img1 = new ImageIcon("src\\Image\\BACKGROUNDS_GE_1.png");
	ImageIcon Jimg = new ImageIcon(img1.getImage().getScaledInstance(800, 600, 4));
	
	public ImageScanner() throws HeadlessException {
		super();
		Toolkit tk = Toolkit.getDefaultToolkit();
		this.setLocation((tk.getScreenSize().width - 800) / 2, (tk
				.getScreenSize().height - 750) / 2);
		this.setSize(800, 700); 
		this.getContentPane().setLayout(null);
		this.setTitle("图片浏览器");
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		btnPanelInit(); 
		PanelInit();
		
		this.add(mainPanel); 
		this.add(btnPanel);  
	}

	private void PanelInit() {
		mainPanel = new JPanel();
		mainPanel.setBounds(0, 0, 800, 600); 
		mainPanel.setLayout(null);
		ImageLabel = new JLabel();
		ImageLabel.setIcon(Jimg);   
		ImageLabel.setBounds(0, 0, 800, 600);
		mainPanel.add(ImageLabel);

	}

	private void btnPanelInit() {
		btnPanel=new JPanel() {  
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void paint(Graphics g) { 
				ImageIcon icon=new ImageIcon("src\\Image\\BACKGROUNDS_MAIN_1.png");
				g.drawImage(icon.getImage(),0,0,800,90,null);
				super.paint(g);
			}
		};
		btnPanel.setOpaque(false);
		btnPanel.setLayout(new FlowLayout());  
		btnPanel.setBounds(0, 600, 800, 100);
		PrevImagebtn=new JButton("前一张");  
		InsertImagebtn = new JButton("打开图片");
		NextImagebtn = new JButton("下一张"); 
		PrevImagebtn.setIcon(new ImageIcon("src\\Image\\prev.png"));
		NextImagebtn.setIcon(new ImageIcon("src\\Image\\next.png"));
		btnPanel.add(PrevImagebtn);
		btnPanel.add(InsertImagebtn);
		btnPanel.add(NextImagebtn); 
		

		InsertImagebtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				fc.setAcceptAllFileFilterUsed(false);
				fc.setCurrentDirectory(new File("D:\\Picture"));
				fc.addChoosableFileFilter(new FileFilter() {
					@Override
					public boolean accept(File f) {
						return Filefiteraccept(f);
					}

					@Override
					public String getDescription() {
						return "JPG ,PNG or GIF Images";
					}

				});
				int returnVal = fc.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					file = fc.getSelectedFile();
					if (file != null) {
						InsertImage(file);
						setTitle(file.getName());
					}
				}

			}

		});
		NextImagebtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (file != null) {
					if (file.getParentFile() != null)
						parentfiles = file.getParentFile().listFiles(
								new java.io.FileFilter() {
									@Override
									public boolean accept(File pathname) {
										return Filefiteraccept(pathname);
									}
								});
					for (int i = 0; i < parentfiles.length - 1; i++) {
						if (parentfiles[i].getName().equalsIgnoreCase(
								file.getName()))
							ImageIndex = i + 1;
					}
					InsertImage(parentfiles[ImageIndex]);
					setTitle(parentfiles[ImageIndex].getName());
					file = parentfiles[ImageIndex];
					if (ImageIndex == parentfiles.length-2){
						JOptionPane.showMessageDialog(null, "没有下一张了", "提示",
								JOptionPane.ERROR_MESSAGE);
						setTitle("图片浏览器");
					}
				}
			}
		});
		PrevImagebtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (file != null) {
					if (ImageIndex == -1){
						JOptionPane.showMessageDialog(null, "这是第一张了", "提示",
								JOptionPane.ERROR_MESSAGE);
						setTitle("图片浏览器");
					}
					else if (file.getParentFile() != null)
						parentfiles = file.getParentFile().listFiles(
								new java.io.FileFilter() {
									@Override
									public boolean accept(File pathname) {
										return Filefiteraccept(pathname);
									}
								});
					for (int i = 0; i < parentfiles.length - 1; i++) {
						if (parentfiles[i].getName().equalsIgnoreCase(
								file.getName()))
							ImageIndex = i - 1;
					}
					InsertImage(parentfiles[ImageIndex]);
					setTitle(parentfiles[ImageIndex].getName());
					file = parentfiles[ImageIndex]; 
				}
			}
		});
		
	}

	public void InsertImage(File f) {
		ImageIcon img = new ImageIcon(f.getAbsolutePath());
		ImageIcon JLabelimg = new ImageIcon(img.getImage().getScaledInstance(
				800, 600, 4));
		ImageLabel.setIcon(JLabelimg);
	}

	public boolean Filefiteraccept(File f) {
		int index = 0;
		for (int i = 0; i < f.getName().length(); i++)
			if (f.getName().charAt(i) == '.')
				index = i;
		String type = f.getName().substring(index);

		if (type.equals(".jpg"))
			return true;
		else if (type.equals(".png"))
			return true;
		else if (type.equals(".gif"))
			return true;
		else if (f.isDirectory())
			return true;
		else
			return false;
	}
} 


 

 

昨天又听说 打包需要个fat插件。。回头找找再试试。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值