package xyy;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShowPicture extends JFrame implements ActionListener {
private int index = 0; //图标标记量
private ArrayList<String> arrayList = null;
JButton onButton = null;
JButton nextButton = null;
Picture picture = null;
Panel p1 = null;
Panel p2 = null;
String[] pathString = null;
ShowPicture() {
p1 = new Panel();
p2 = new Panel();
arrayList = new ArrayList<String>();
//得到图片地址
pathString = new String[100];
Properties pp = System.getProperties();
File file = new File((String)pp.get("user.dir"));
File[] files = file.listFiles();
for(File f : files) {
if(f.toString().endsWith(".jpg")) {
arrayList.add(f.getName());
}
}
onButton = new JButton("上一张");
nextButton = new JButton("上一张");
onButton.addActionListener(this);
nextButton.addActionListener(this);
p1.add(onButton);
p1.add(nextButton);
//显示设置
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
this.setSize((int)dimension.getWidth()/2, (int)dimension.getHeight()/2);
this.setLocation((int)dimension.getWidth()/4, (int)dimension.getHeight()/4);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.CENTER);
if(arrayList.size() > 0) {
picture = new Picture("1.jpg");
picture.setSize(140, 105);
p2.add(picture);
}
this.setVisible(true);
System.out.println(arrayList.size());
}
public void actionPerformed(ActionEvent e) {
JButton jbt = (JButton)e.getSource();
if(jbt == onButton) {
if(index >= arrayList.size() -2) {
index = arrayList.size() -2;
}
if(index <= arrayList.size() -1) {
p2.remove(picture);
this.setVisible(true);
picture = new Picture(arrayList.get(index));
picture.setSize(140, 105);
p2.add(picture);
index++;
this.setVisible(true);
}
} else if(jbt == nextButton) {
if(index <= 0) {
index = 1;
}
if(index >= 0) {
p2.remove(picture);
this.setVisible(true);
picture = new Picture(arrayList.get(index));
picture.setSize(140, 105);
p2.add(picture);
index--;
this.setVisible(true);
}
}
}
class Picture extends JPanel {
Image image = null;
ImageIcon img = null;
Picture(String filename) {
img = new ImageIcon(filename);
image = img.getImage();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(130, 130);
}
}
public void onPictrue() {
System.out.println("1");
new Picture(arrayList.get(index));
index++;
}
public void nextPictrue() {
System.out.println("2");
new Picture(arrayList.get(index));
index--;
}
public static void main(String[] args) {
new ShowPicture();
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public ArrayList<String> getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}
}
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Properties;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ShowPicture extends JFrame implements ActionListener {
private int index = 0; //图标标记量
private ArrayList<String> arrayList = null;
JButton onButton = null;
JButton nextButton = null;
Picture picture = null;
Panel p1 = null;
Panel p2 = null;
String[] pathString = null;
ShowPicture() {
p1 = new Panel();
p2 = new Panel();
arrayList = new ArrayList<String>();
//得到图片地址
pathString = new String[100];
Properties pp = System.getProperties();
File file = new File((String)pp.get("user.dir"));
File[] files = file.listFiles();
for(File f : files) {
if(f.toString().endsWith(".jpg")) {
arrayList.add(f.getName());
}
}
onButton = new JButton("上一张");
nextButton = new JButton("上一张");
onButton.addActionListener(this);
nextButton.addActionListener(this);
p1.add(onButton);
p1.add(nextButton);
//显示设置
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
this.setSize((int)dimension.getWidth()/2, (int)dimension.getHeight()/2);
this.setLocation((int)dimension.getWidth()/4, (int)dimension.getHeight()/4);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.NORTH);
this.add(p2, BorderLayout.CENTER);
if(arrayList.size() > 0) {
picture = new Picture("1.jpg");
picture.setSize(140, 105);
p2.add(picture);
}
this.setVisible(true);
System.out.println(arrayList.size());
}
public void actionPerformed(ActionEvent e) {
JButton jbt = (JButton)e.getSource();
if(jbt == onButton) {
if(index >= arrayList.size() -2) {
index = arrayList.size() -2;
}
if(index <= arrayList.size() -1) {
p2.remove(picture);
this.setVisible(true);
picture = new Picture(arrayList.get(index));
picture.setSize(140, 105);
p2.add(picture);
index++;
this.setVisible(true);
}
} else if(jbt == nextButton) {
if(index <= 0) {
index = 1;
}
if(index >= 0) {
p2.remove(picture);
this.setVisible(true);
picture = new Picture(arrayList.get(index));
picture.setSize(140, 105);
p2.add(picture);
index--;
this.setVisible(true);
}
}
}
class Picture extends JPanel {
Image image = null;
ImageIcon img = null;
Picture(String filename) {
img = new ImageIcon(filename);
image = img.getImage();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(130, 130);
}
}
public void onPictrue() {
System.out.println("1");
new Picture(arrayList.get(index));
index++;
}
public void nextPictrue() {
System.out.println("2");
new Picture(arrayList.get(index));
index--;
}
public static void main(String[] args) {
new ShowPicture();
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public ArrayList<String> getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}
}