java课设代码 java代码教学( 三 )


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() {
.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);
【java课设代码 java代码教学】}
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) {
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();

推荐阅读