java图片浏览器代码 java实现浏览器( 四 )


for (String s : list) {
String path = directory + "\\" + s;
File temp = new File(path);
if (!temp.isDirectory()) {
this.paths.add(path);
}
}
}
}
public void changePage(int page) {
if (!paths.isEmpty()) {
switch (showType) {
case GRID:
this.page += page;
if (this.page == 0) {
this.page = 1;
}
if (this.pagepageMax) {
this.page = pageMax;
}
break;
case LARGE:
this.count += page;
if (count0) {
count = 0;
}
if (countpaths.size() - 1) {
count = paths.size() - 1;
}
break;
}
this.showingImage();
}
}
public void showingImage() {
this.remove(showingPanel);
showingPanel = new JPanel();
int size = paths.size();
int max = MAX_R * MAX_C;
pageMax = (size % max == 0 ? size / max : size / max + 1);
switch (showType) {
case GRID:
this.setBounds(100, 100, G_WIDTH, G_HEIGHT);
showingPanel.setLayout(new GridLayout(MAX_R, MAX_C - 1));
int i = (page - 1) * max;
int j = page * max;
for (; ijisize; i++) {
String path = paths.get(i);
showingPanel.add(new ImagePanel(path));
}
break;
case LARGE:
int left = count - 1;
int right = count + 1;
this.setBounds(100, 100, L_WIDTH, L_HEIGHT);
showingPanel.setLayout(new GridLayout(1, 2));
if (left = 0) {
showingPanel.add(new ImagePanel(paths.get(left)));
} else {
showingPanel.add(new ImagePanel(""));
}
showingPanel.add(new ImagePanel(paths.get(count)));
if (rightsize) {
showingPanel.add(new ImagePanel(paths.get(right)));
} else {
showingPanel.add(new ImagePanel(""));
}
break;
}
this.add(showingPanel, BorderLayout.CENTER);
showingPanel.updateUI();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ImageGallery();
}
class ImagePanel extends JPanel {
private ImageIcon image;
ImagePanel(String path) {
image = new ImageIcon(path);
}
public ImageIcon getImageIcon() {
return this.image;
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(image.getImage(), 0, 0, this.getWidth(), this
.getHeight(), this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String key = e.getActionCommand();
System.out.println(key);
if (key.equals("open")) {
doOpen();
} else if (key.endsWith("last")) {
changePage(-1);
} else if (key.endsWith("next")) {
changePage(1);
} else if (key.endsWith("large")) {
if (!paths.isEmpty()) {
count =0;
showType = LARGE;
showingImage();
}
} else if (key.endsWith("grid")) {
if (!paths.isEmpty()) {
page = 1;
showType = GRID;
showingImage();
}
}
}
}
简单的java图片浏览器,使图片居中显示有两种方法:
1. 将图片缩放,也就是显示缩略图
2. 使用JScrollPane,显示滚动条
第一种比较复杂,这里给出第二种方法的实现:
将你代码的第17、18行改成:
label = new JLabel();
JScrollPane scroll = new JScrollPane(label);
add(scroll);
10R,求个简单的JAVA图片浏览器,窗体我设计好了,只需要,下一张 , 上一张按钮具有功能 。,急自定义一个jpanel用于展示image
/**
*
*/
package org.demo;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.HashMap;
import java.util.Map;

推荐阅读