java中图片浏览器代码 java中图片浏览器代码有哪些( 三 )


if(sf==null || sf.length==0)return;
pl.setIcon(new ImageIcon(sf[index].getAbsolutePath()));
System.out.println(sf[index].getAbsolutePath());
}
}
简单的java图片浏览器,使图片居中显示有两种方法:
1. 将图片缩放,也就是显示缩略图
2. 使用JScrollPane , 显示滚动条
第一种比较复杂,这里给出第二种方法的实现:
将你代码的第17、18行改成:
label = new JLabel();
JScrollPane scroll = new JScrollPane(label);
add(scroll);
用java编写一个图片浏览器import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class ImageGallery extends JFrame implements ActionListener {
public static final int G_WIDTH = 400, G_HEIGHT = 620;
public static final int L_WIDTH = 1200, L_HEIGHT = 520;
public static final int MAX_R = 3, MAX_C = 2;
public static final int GRID = 1, LARGE = 2;
private JFileChooser chooser;
private JMenuBar toolBar;
private JMenu menu;
private JMenuItem open;
private ArrayListString paths;
private JPanel showingPanel, buttonPanel;
private int page = 1;
private int count = 0;
private int showType = GRID;
private int pageMax;
private JButton last, next, large, grid;
public ImageGallery() {
this.setTitle("Image gallery 0.1");
this.setBounds(100, 100, G_WIDTH, G_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
toolBar = new JMenuBar();
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
menu = new JMenu("File");
toolBar.add(menu);
open = new JMenuItem("open");
open.addActionListener(this);
menu.add(open);
paths = new ArrayListString();
this.setJMenuBar(toolBar);
showingPanel = new JPanel();
buttonPanel = new JPanel();
last = new JButton("last");
last.addActionListener(this);
next = new JButton("next");
next.addActionListener(this);
large = new JButton("large");
large.addActionListener(this);
grid = new JButton("grid");
grid.addActionListener(this);
buttonPanel.add(last);
buttonPanel.add(large);
buttonPanel.add(grid);
buttonPanel.add(next);
this.add(buttonPanel, BorderLayout.SOUTH);
this.add(showingPanel);
this.setVisible(true);
}
public void doOpen() {
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
String directory = chooser.getSelectedFile().getPath();
this.getAllImage(directory);
for (String s : this.paths) {
System.out.println(s);
}
this.showingImage();
}
}
public void getAllImage(String directory) {
paths.clear();
page = 1;
count = 0;
File file = new File(directory);
if (file.isDirectory()) {
String[] list = file.list();
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) {

推荐阅读