[JAVA]Swing、事件监听、文件的初级综合。简易图片浏览器,逸雨清风XIUXIU。

JAVA的SWING、事件处理和文件打开,与VS各有千秋。

图片浏览应用,打开图片,按钮和键盘控制当前文件夹里上下一张图片,将所有图片缩放成适合屏幕显示的尺寸。

//发现一个问题是好像有内存溢出,打开很多图片后就会显示虚拟机内存不足,但是每次显示图片的时候都重新new lable了的。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.jar.Attributes.Name;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class XIUXIU extends JFrame
{
	JPanel panel,panel2;
	JLabel labelphoto;
	JButton buttonlast,buttonnext,buttonopen,buttonexit;
	ImageIcon icon = null; //用陈年墨色,画成她模样
	String path;  // 图片所在文件夹路径
	String[] fileNames; //文件列表
	int i=0,count=0;
	
	public XIUXIU()
	{
		super("逸雨清风XIUXIU");
		buttonlast = new JButton("上一张");
		buttonnext = new JButton("下一张");
		buttonopen = new JButton("打开");
		buttonexit = new JButton("退出");
		panel = new JPanel(new GridLayout(1,4));    //面板里面采用网格布局,划分为四个按钮
		panel.add(buttonopen);panel.add(buttonlast);panel.add(buttonnext);panel.add(buttonexit);
		buttonopen.addKeyListener(new XIUKeyListener());
		buttonopen.addActionListener(new OpenListener());
		buttonexit.addActionListener(new ExitListener());
		buttonnext.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				if (i<count-1) i++;
				else if (i == count-1) i=0;	//初见的时光
				icon = new ImageIcon(path+"\\"+fileNames[i]);
				XIUXIU.this.setTitle(fileNames[i]);
				ShowPhoto(icon);
			}
		});
		buttonlast.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) {
				if (i>0) i--;
				else if (i == 0) i=count-1;		//初见的时光
				icon = new ImageIcon(path+"\\"+fileNames[i]);
				XIUXIU.this.setTitle(fileNames[i]);
				ShowPhoto(icon);
			}
		});
		this.add(panel,BorderLayout.SOUTH);         //把按钮面板用边界布局放在窗口底部
		this.setIconImage(Toolkit.getDefaultToolkit().createImage("logo.png")); //更换图标
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		this.setSize(screenSize.width,screenSize.height);
		//this.setResizable(false); //设置窗口不可改变
		//this.setExtendedState(this.MAXIMIZED_BOTH); //窗口最大化
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public class OpenListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			OpenFile();
		}
	}
	public class ExitListener implements ActionListener
	{
	public void actionPerformed(ActionEvent e)
	{
		System.exit(1);
	}
	}
	public class XIUKeyListener extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			int key = e.getKeyCode();
			if (key == KeyEvent.VK_LEFT)
			{
				if (i>0) i--;
				else if (i == 0) i=count-1;	

	
				icon = new ImageIcon(path+"\\"+fileNames[i]);
				XIUXIU.this.setTitle(fileNames[i]);
				ShowPhoto(icon);
			}else if (key == KeyEvent.VK_RIGHT)
			{
				if (i<count-1) i++;
				else if (i == count-1) i=0;	

	
				icon = new ImageIcon(path+"\\"+fileNames[i]);
				XIUXIU.this.setTitle(fileNames[i]);
				ShowPhoto(icon);
			}
		}
	}
	public void OpenFile()
	{
		JFileChooser fChooser = new JFileChooser();
		int rVal = fChooser.showOpenDialog(this);
		if (rVal == JFileChooser.APPROVE_OPTION) //确定打开
		{
			String filename = fChooser.getSelectedFile().getName();
			this.setTitle(filename);
			path = fChooser.getCurrentDirectory().toString();  //文件所在文件夹路径
			File file = new File(path);
			if (file.exists() && file.isDirectory())    //路径存在且为目录
			{
				i=0;count=0;  //记录当前文件夹图片总数,防止数组越界
				fileNames = null;
				fileNames = file.list(new FilenameFilter() 
				{
					public boolean accept(File dir, String name) 
					{
					return (name.endsWith(".png") || name.endsWith(".jpg")|| name.endsWith(".jpeg")|| name.endsWith(".gif")|| name.endsWith

(".bmp") );
					}
				});		//过滤器抓取图片文件
				for (String temp:fileNames) count++;
			}
			icon = new ImageIcon(fChooser.getSelectedFile().getPath());
			ShowPhoto(icon);
		}
	}
	public void ShowPhoto(ImageIcon icon)
	{
		this.setVisible(true);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		double xphoto = icon.getIconWidth();
		double yphoto = icon.getIconHeight();
		double xscreen = screenSize.width;
		double yscreen = screenSize.height;
		double temp = 0; 
		
		temp = yphoto/yscreen;
		yphoto = yscreen * 0.9;
		xphoto =  (xphoto/temp)*0.9; 
		icon.setImage(icon.getImage().getScaledInstance((int)xphoto,(int)yphoto,Image.SCALE_DEFAULT));  //图片缩放成全屏算法
		
		labelphoto = new JLabel();
		labelphoto.setIcon(icon);		//从此用我双眼,替你看着世界
		labelphoto.setIcon(icon);
		panel2 = new JPanel();
		panel2.add(labelphoto);
		this.add(panel2);
		this.setVisible(true);
	}
	public static void main(String[] args)
	{
		XIUXIU xiuxiu = new XIUXIU();
		xiuxiu.setVisible(true);
	}
}



 /*

******逸雨清风 出品

******http://blog.csdn.net/xyydyyqf

*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸雨清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值