java简单图片查看代码

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;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值