请给我的JAVA程序添加一个自动播放图片功能

我写了一个JAVA窗口代码,请给这个这段代码添加一个功能,让它能自动播放电脑内任何一个文件夹里的所有图片,谢谢
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
*
* @author Administrator
*/
public class Main {

/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
final Frame f = new Frame("毕业纪念册");// 标题
final JLabel lbl = new JLabel();
f.setSize(600, 400);// 对话框大小
f.setLocation(100, 100);// 对话框初始位置
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
MenuBar mb = new MenuBar();
Menu m1 = new Menu("photolist");
Menu m2 = new Menu("video栏");
MenuItem mi1 = new MenuItem("photo1");
MenuItem mi2 = new MenuItem("open");
mi2.addActionListener(new ActionListener() { // 打开文件夹

public void actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(f, "photo open",
FileDialog.LOAD);
fd.show();

String fileName = fd.getDirectory() + fd.getFile();
Icon icon = new ImageIcon(fileName,"");
lbl.setIcon(icon);
}
});
MenuItem mi3 = new MenuItem("photo3");
MenuItem mi4 = new MenuItem("photo4");
MenuItem mi5 = new MenuItem("video1");
MenuItem mi6 = new MenuItem("video2");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
f.setMenuBar(mb);
f.add(lbl);
f.show();
}

}
做的效果 就是每隔几秒 换一张。我想做个毕业纪念册,最好能播放VIDEO,呵呵,大大们看怎么设计啦,最符合要求的加分,能加多少加多少

//其余的类在qq上给你..
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ImageFrame extends JFrame implements ActionListener, MouseListener {
/**
*
*/
private static final long serialVersionUID = -5241799989073556019L;

static Properties property = new Properties();
static {
Class iClass;
try {
iClass = Class.forName("sun.awt.im.InputMethodContext");
Field field = iClass.getDeclaredField("belowTheSpotInputRequested");
AccessibleObject.setAccessible(new AccessibleObject[] { field },
true);
field.setBoolean(null, false);

} catch (Exception e) {

}

}

public JPanel imagePanel;

public JPanel buttonPanel;

public JPanel statePanel;

public JLabel imageLabel;

public JLabel stateLabel;

public JButton before;

public JButton play;

public JButton stop;

public JButton next;

public JButton bigger;

public JButton smaller;

static JTextArea area = new JTextArea(5, 60);

JScrollPane scroll = new JScrollPane(area);

public File picFile;

public Image img;

public ImageIcon imageIcon;

public String fileParent;

FileNameExtensionFilter imageFilter = new FileNameExtensionFilter(null,
"jpeg", "jpg", "png", "gif");

JFileChooser imageChooser = new JFileChooser();

public File filePath[];

public File imagePath[];

public static Picture images[];

public int imageFileNumber = 0;

public static int locationImage = 0;

public static ImageFrame myImageFrame;

private PlayTimer playTimer;

public Image[] imageOffer;

public int scale = 8;

public JScrollPane imageScrollPane;

public ImageFrame(String picPath) {

picFile = new File(picPath);
fileParent = picFile.getParent();
filePath = (new File(fileParent)).listFiles();
try {
img = javax.imageio.ImageIO.read(picFile);

} catch (IOException ex) {
ex.printStackTrace();
}

imageIcon = new ImageIcon(img);
imageLabel = new JLabel(imageIcon);
imagePanel = new JPanel(new BorderLayout());
imagePanel.setBackground(Color.black);
//R:238 G:243 B:250
//imagePanel.setBackground(new Color(238,243,250));
stateLabel = new JLabel();

final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try {
UIManager.setLookAndFeel(windows);
SwingUtilities.updateComponentTreeUI(this);// 更新控件的外观

} catch (Exception e) {
e.printStackTrace();
}

setLayout(new BorderLayout());
setTitle("PowerSee 图片查看器");
setIconImage(new ImageIcon("icon/powerSee.png").getImage());
setSize(800, 600);
this.setMinimumSize(new Dimension(600, 400));
setVisible(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
buttonPanel = makeButtonPanel();

area.setLineWrap(true);
// area.setWrapStyleWord(true);
area.addMouseListener(this);

// area.setFont(new Font(area.getFont().getFamily(), Font.PLAIN,
// 18));
LookAndFeel.installColorsAndFont(area, "Label.background",
"Label.foreground", "TextArea.font");

area.setBorder(BorderFactory.createTitledBorder("此处可添加照片描述:"));

area.setLineWrap(true);
area.setWrapStyleWord(true);
area.getDocument().addDocumentListener(new SWING_OnValueChanged());

/*
area.setText("");
area.setText("像素大小: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
+ " 文件大小: " + picFile.length() / 1024 + "KB");
*/
/*
area.getDocument()
.addDocumentListener(new SWING_OnValueChanged());
*/

// area.setBackground(new Color() );
Font font = new Font("宋体", Font.PLAIN, 17);
area.setFont( font);
// area.setEditable(false);

statePanel = new JPanel();
//:36 G:53 B:71R:192 G:192 B:196
statePanel.setBackground(new Color(192,192,196));
stateLabel.setText("像素大小: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
+ " 文件大小: " + picFile.length() / 1024 + "KB");
//stateLabel;
imagePanel.add(imageLabel, BorderLayout.CENTER);

imageScrollPane = new JScrollPane(imagePanel);

add(imageScrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.NORTH);
//statePanel.add(stateLabel);
//statePanel.setBackground(Color.black);
statePanel.add(area, BorderLayout.CENTER);//scroll

add(statePanel, BorderLayout.SOUTH);

imagePanel.repaint();
imagePanel.validate();

Load();
}

//对于此法可借鉴,返回一个panel竟然......
public JPanel makeButtonPanel() {
JPanel aButtonPanel = new JPanel();

before = new JButton("前一张");
next = new JButton("下一张");
play = new JButton("自动播放");
stop = new JButton("停止");
bigger = new JButton("放大");
smaller = new JButton("缩小");

stop.setEnabled(false);

before.addActionListener(this);
next.addActionListener(this);
play.addActionListener(this);
stop.addActionListener(this);
bigger.addActionListener(this);
smaller.addActionListener(this);

aButtonPanel.add(before);
aButtonPanel.add(play);
aButtonPanel.add(stop);
aButtonPanel.add(next);
aButtonPanel.add(smaller);
aButtonPanel.add(bigger);

return aButtonPanel;
}

/*
* //imageFileNumber 个图像文件
//filePath.length 个文件

//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素

*/
public void Load() {

imageChooser.setFileFilter(imageFilter);
//System.out.println(filePath.length+"----");

//filePath.length,也就是Image这个文件夹里有多少个文件.......所有种类的文件....!!

for (int i = 0; i < filePath.length; i++) {

if (!filePath[i].isDirectory() && imageFilter.accept(filePath[i])) {
imageFileNumber++;
} else {
filePath[i] = null;
}
}

//imageFileNumber 个图像文件
//filePath.length 个文件

//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素

imagePath = new File[imageFileNumber];

images = new Picture[imageFileNumber];

imageFileNumber = 0;
for (int i = 0; i < filePath.length; i++) {
if (filePath[i] != null) {

imagePath[imageFileNumber++] = filePath[i];

}
}

imageFileNumber--;
for (int i = 0; i < imagePath.length; i++) {
if (imagePath[i] == picFile) {
locationImage = i;
}

}
for (int i = 0; i < imagePath.length; i++) {

initTxt(imagePath[i], i);

}

}

public void initTxt(File picFile, int i) {

try {

property.load(new FileInputStream("a.properties"));

/*
String txt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());
*/

images[i] = new Picture(imagePath[i].getParent() + "."
+ imagePath[i].getName(), "");

} catch (Exception e) {

}

//初始化图片的时候,要从资源文件里读取所有文件的txt信息,可能花费时间较长

}

/*
*
//imageFileNumber 个图像文件
//filePath.length 个文件

//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素

*/
public void Before() {
scale = 8;
if (--locationImage < 0) {
locationImage = imageFileNumber;
}
try {
img = javax.imageio.ImageIO.read(imagePath[locationImage]);
} catch (IOException ex) {
ex.printStackTrace();
}
imageIcon.setImage(img);
imageLabel.setIcon(imageIcon);
picFile = imagePath[locationImage];

/*
String text = "像素大小: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
+ " 文件大小: " + picFile.length() / 1024 + "KB";
*/

//System.out.println(picFile.toString());

//Picture p = new Picture(picFile, text);

try {

Properties property = new Properties();
property.load(new FileInputStream("a.properties"));

String areaTxt = "";

areaTxt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());

area.setText("");
area.setText(areaTxt);

} catch (Exception e) {

}

imagePanel.repaint();
imagePanel.validate();
imageScrollPane.repaint();
imageScrollPane.validate();
}

/*
* //imageFileNumber 个图像文件
//filePath.length 个文件

//filePath 以某个文件夹的所有文件为元素的数组, 以File类型为元素
//imagePath 以所有图像文件为元素的数组,仍然以File类型为元素

*/
public void Next() {
scale = 8;
if (++locationImage > imageFileNumber) {
locationImage = 0;
}
try {
img = javax.imageio.ImageIO.read(imagePath[locationImage]);
} catch (IOException ex) {
ex.printStackTrace();
}
imageIcon.setImage(img);
imageLabel.setIcon(imageIcon);
picFile = imagePath[locationImage];

try {

Properties property = new Properties();
property.load(new FileInputStream("a.properties"));

String areaTxt = "";
areaTxt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());

area.setText("");
area.setText(areaTxt);

} catch (Exception e) {

}

imagePanel.repaint();
imagePanel.validate();
imageScrollPane.repaint();
imageScrollPane.validate();
}

public void Bigger() {
ImageIcon icon = imageIcon;
if (scale <= 17) {
ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(),
icon.getIconWidth() / 8, icon.getIconHeight() / 8, ++scale)
.getImage());
imageLabel.setIcon(tmpicon);
} else {
return;
}
}

public void Smaller() {
ImageIcon icon = imageIcon;
if (scale > 1) {
ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(),
icon.getIconWidth() / 8, icon.getIconHeight() / 8, --scale)
.getImage());
imageLabel.setIcon(tmpicon);
} else {
return;
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(before)) {
Before();
}
if (e.getSource().equals(next)) {
Next();
}
if (e.getSource().equals(play)) {
stop.setEnabled(true);
before.setEnabled(false);
next.setEnabled(false);
bigger.setEnabled(false);
smaller.setEnabled(false);
play.setEnabled(false);
playTimer = new PlayTimer(this);
playTimer.start();
}
if (e.getSource().equals(stop)) {
stop.setEnabled(false);
before.setEnabled(true);
next.setEnabled(true);
bigger.setEnabled(true);
smaller.setEnabled(true);
play.setEnabled(true);
playTimer.cancel();
}
if (e.getSource().equals(bigger)) {
Bigger();
}
if (e.getSource().equals(smaller)) {
Smaller();
}
}

public void mouseClicked(MouseEvent arg0) {
// 鼠标点击
area.setEditable(true);
// pane.add(scroll, BorderLayout.CENTER);
}

public void mouseExited(MouseEvent arg0) {
// 鼠标离开区域
area.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); //鼠标离开Text区后恢复默认形态
area.setBackground(null);

//保存信息

//area.setEditable(false);
}

public void mousePressed(MouseEvent arg0) {

}

public void mouseReleased(MouseEvent arg0) {

// 鼠标释放
}

public void mouseEntered(MouseEvent arg0) {
// 鼠标释进入区域
area.setCursor(new Cursor(Cursor.TEXT_CURSOR)); //鼠标进入Text区后变为文本输入指针

area.setBackground(new Color(255, 255, 170));
}

protected void processWindowEvent(WindowEvent e) {

super.processWindowEvent(e);

if (e.getID() == WindowEvent.WINDOW_CLOSING) {
try {
} catch (Exception ex) {

}
System.exit(0);

}

}

public static void main(String a[]) {

new ImageFrame("Image/p1.jpg");
}
}

class Picture {
String text;

String picFile;

Picture(String string, String text) {
this.picFile = string;
this.text = text;

}

String getText() {
return this.text;
}

String getFilePath() {
return this.picFile;
}

void setText(String text) {
this.text = text;
}

}

class SWING_OnValueChanged implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
textValueChanged(e);
}

public void insertUpdate(DocumentEvent e) {
textValueChanged(e);
}

public void removeUpdate(DocumentEvent e) {
textValueChanged(e);
}

public void textValueChanged(DocumentEvent evt) {
//images
int i = 0;
System.out.println("Swing文本框的内容改变了!" + ImageFrame.locationImage);

i = ImageFrame.locationImage;

//System.out.println("图片!" + ImageFrame.images[i].getFilePath());

try {

ImageFrame.property.setProperty(ImageFrame.images[i].getFilePath(),
ImageFrame.area.getText());
// ImageFrame.images[i].setText(ImageFrame.area.getText());
ImageFrame.property.store(new FileOutputStream("a.properties"),
"a.properties");

} catch (Exception ex) {

}

}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-01
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;

/**
*
* @author Administrator
*/
public class Main {

public static void main(String[] args) {
// TODO code application logic here
final Frame f = new Frame("毕业纪念册");// 标题
final JLabel lbl = new JLabel();
f.setSize(600, 400);// 对话框大小
f.setLocation(100, 100);// 对话框初始位置
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
MenuBar mb = new MenuBar();
Menu m1 = new Menu("photolist");
Menu m2 = new Menu("video栏");
MenuItem mi1 = new MenuItem("photo1");
MenuItem mi2 = new MenuItem("open");
mi2.addActionListener(new ActionListener() { // 打开文件夹

public void actionPerformed(ActionEvent e) {
final JFileChooser fd = new JFileChooser();
fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

fd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (fd.getSelectedFile() != null) {
String fileName = fd.getSelectedFile()
.getAbsolutePath();
java.io.File dir = new java.io.File(
fileName);
if (dir.isDirectory()) {
java.io.File[] files = dir.listFiles();
int len = files.length;

for (int i = 0; i < len; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch
// block
e1.printStackTrace();
}
String fname = files[i]
.getAbsolutePath();

if (fname.endsWith(".jpeg")
|| fname.endsWith(".jpg")) {
System.out.println(fname);
lbl.removeAll();
Icon icon = new ImageIcon(
fname, "");
lbl.setIcon(icon);
lbl.update(f.getGraphics());

}

}

}
}
}

});
fd.showOpenDialog(f);

}
});
MenuItem mi3 = new MenuItem("photo3");
MenuItem mi4 = new MenuItem("photo4");
MenuItem mi5 = new MenuItem("video1");
MenuItem mi6 = new MenuItem("video2");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
f.setMenuBar(mb);
f.add(lbl);
f.show();
}

}
第2个回答  2010-04-01
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* @author Hardneedl
*/
final class PicturePlayer extends JFrame {
public String getTitle() {return "Pictures Player";}
static private final Dimension size = new Dimension(600, 400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize() {return size;}

private File[] files;
private Image img;
private int index=-1;
private Timer timer=new Timer(1000,new TimerAction());

private JComponent canvas = new JComponent(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(files!=null&&index>=0&&index< files.length-1){

try {
img = ImageIO.read(files[index]);
} catch(IOException e) {
e.printStackTrace();
}

if (img==null)return;
Graphics gg = g.create();
gg.drawImage(img,0,0,getWidth(),getHeight(),this);
gg.dispose();
}
}
};

PicturePlayer() throws HeadlessException {
init();
attachListeners();
doLay();
}

private void init() {
}

private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void doLay() {
Container container = getContentPane();
JMenu fileMenu = new JMenu("File");
fileMenu.add(new OpenFileAction());
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
container.add(menuBar,BorderLayout.NORTH);
container.add(canvas,BorderLayout.CENTER);
pack();
setVisible(true);
}

private class OpenFileAction extends AbstractAction {
private OpenFileAction() {super("Select Directory...");}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(".");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(chooser.showOpenDialog(PicturePlayer.this)==JFileChooser.APPROVE_OPTION){
File f= chooser.getSelectedFile();
if (f!=null&&f.isDirectory()) {
files=f.listFiles();
index=files!=null&&files.length>0?0:-1;
if (files!=null&&files.length>0){
timer.start();
}
else {
timer.stop();
}
}
}
}
}

private class TimerAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
index++;
if (index>=files.length){
index=0;
}
canvas.paintImmediately(canvas.getBounds());
}
}

public static void main(String[] args) {new PicturePlayer();}
}
第3个回答  2010-04-01
晚上回去了给你写
第4个回答  2010-04-01
做的效果 就是每隔几次 换一张 这样是么?
相似回答