[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
*/
相關文章
- 監聽瀏覽器的後退事件瀏覽器事件
- 監聽瀏覽器返回,pushState,popstate 事件,window.history物件瀏覽器事件物件
- 初識事件監聽事件
- JXImageBrowser (圖片瀏覽器)瀏覽器
- iOS圖片瀏覽器 - XLPhotoBrowser(類似微信多圖片瀏覽效果)iOS瀏覽器
- WebView 詳解(簡易瀏覽器Demo)WebView瀏覽器
- 事件和事件監聽器事件
- 關於瀏覽器裡事件的捕獲和冒泡及監聽器執行的順序瀏覽器事件
- iSee Pro for Mac圖片瀏覽器Mac瀏覽器
- GraphicConverter for Mac(圖片瀏覽器)Mac瀏覽器
- 幻燈片式圖片瀏覽器瀏覽器
- passive 的事件監聽器事件
- 一起擼個朋友圈吧 - 圖片瀏覽(中)【圖片瀏覽器】瀏覽器
- 簡易筆記:瀏覽器快取策略筆記瀏覽器快取
- JS事件監聽器JS事件
- 23.Quick QML-簡單且好看的圖片瀏覽器-支援多個圖片瀏覽、縮放、旋轉、滑輪切換圖片UI瀏覽器
- 無邊框輕量級圖片瀏覽器:LilyView for mac瀏覽器ViewMac
- Electron為檔案瀏覽器建立圖示(三)瀏覽器
- 高效圖片瀏覽器:Pixea Plus for Mac瀏覽器Mac
- GKPhotoBrowser--自定義圖片瀏覽器瀏覽器
- 圖片瀏覽器--學習筆記瀏覽器筆記
- 基石-初見瀏覽器(一):瀏覽器渲染瀏覽器
- IE瀏覽器下圖片載入onload事件失效解決方案瀏覽器事件
- java 自定義監聽事件Java事件
- 瀏覽器事件解析瀏覽器事件
- 使用瀏覽器事件瀏覽器事件
- 如何移除事件監聽器事件
- 相容所有瀏覽器的圖片上傳本地預覽效果瀏覽器
- 瀏覽器 Web 訪問剪下板圖片瀏覽器Web
- FotoTime Mac(圖片瀏覽管理器)Mac
- 如何實現一個圖片瀏覽器瀏覽器
- html背景圖片自適應瀏覽器HTML瀏覽器
- onscroll事件的瀏覽器支援事件瀏覽器
- 跨瀏覽器的事件代理瀏覽器事件
- chrome瀏覽器不能顯示本地圖片辦法解決(圖片預覽)Chrome瀏覽器地圖
- 不同瀏覽器下圖片滾動效果的js瀏覽器JS
- ISO開發基礎—簡單的圖片瀏覽appAPP
- java 監聽 redis 過期事件JavaRedis事件