下面是一个例子
import?java.awt.BorderLayout;
import?java.awt.Component;
import?java.net.MalformedURLException;
import?java.net.URL;
import?javax.media.Manager;
import?javax.media.MediaLocator;
import?javax.media.Player;
import?javax.swing.JFileChooser;
import?javax.swing.JFrame;
public?class?MediaPlayer?extends?javax.swing.JPanel?{
public?MediaPlayer(URL?mediauUrl)?{??
initComponents();
setLayout(new?BorderLayout());
try?{
Player?mediaPlayer?=?Manager.createRealizedPlayer(new?MediaLocator(mediauUrl));
Component?video?=?mediaPlayer.getVisualComponent();
Component?control?=?mediaPlayer.getControlPanelComponent();
if?(video?!=?null)?{
add(video,?BorderLayout.CENTER);??//?place?the?video?component?in?the?panel
}
add(control,?BorderLayout.SOUTH);??//?place?the?control?in?panel
mediaPlayer.start();
}?catch?(Exception?e)?{
private?void?initComponents()?{
javax.swing.GroupLayout?layout?=?new?javax.swing.GroupLayout(getTopLevelAncestor());
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
);
layout.setVerticalGroup(
public?static?void?main(String[]?args)?{
JFileChooser?fileChooser?=?new?JFileChooser();
fileChooser.showOpenDialog(null);
URL?mediaUrl?=?null;
mediaUrl?=?fileChooser.getSelectedFile().toURI().toURL();
}?catch?(MalformedURLException?ex)?{
System.out.println(ex);
JFrame?mediaTest?=?new?JFrame("Movie?Player");
mediaTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MediaPlayer?mediaPanel?=?new?MediaPlayer(mediaUrl);
mediaTest.add(mediaPanel);
mediaTest.setLocationRelativeTo(null);
mediaTest.setVisible(true);
使用两种不同的方法实现图片预览功能
Java代码
BODY
script language="javascript"
function ShowImage(path){
function test(){
document.all.showimg.src=document.all.file1.value;
/script
input type="file" id="file1" onchange="test()"br/
/BODY
"执行的程序" 文件路径
//注意一:程序的路径要打引号,我的迅雷看看在f盘 ,文件在e盘.
//注意二:程序和文件夹路径中间有个空格.
根据下面的代码自己修改吧
然后把改名后的照片文件放到你的项目名的目录下,比如你的项目名叫"slideshow",你就把照片文件放到slideshow文件夹下.
最后把下面的Java程序拷贝到你的项目中,把有DD类名的地方改成你的类名,就行了.
完整的让一些照片在JFrame窗体里自动随机播放的幻灯片程序如下
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class DD extends JFrame implements Runnable{
ImageIcon ii=new ImageIcon("photo1.jpg");//这里换成你的图片文件名,放在你的项目名的文件夹中
DD(){
super("Slide");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
public void paint(Graphics g){
super.paint(g);
@Override
public void run() {
while(true){
try {
} catch (InterruptedException e) {
e.printStackTrace();
this.repaint();
public static void main(String[] args) {
DD d=new DD();
Thread t=new Thread(d);
t.start();
//其余的类在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.JButton;
import javax.swing.JFileChooser;
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 {
/**
*
*/
static Properties property = new Properties();
static {
Class iClass;
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;
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 JScrollPane imageScrollPane;
public ImageFrame(String picPath) {
picFile = new File(picPath);
fileParent = picFile.getParent();
filePath = (new File(fileParent)).listFiles();
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);
stateLabel = new JLabel();
final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(windows);
SwingUtilities.updateComponentTreeUI(this);// 更新控件的外观
setLayout(new BorderLayout());
setTitle("PowerSee 图片查看器");
setIconImage(new ImageIcon("icon/powerSee.png").getImage());
setExtendedState(JFrame.MAXIMIZED_BOTH);
buttonPanel = makeButtonPanel();
area.setLineWrap(true);
// area.setWrapStyleWord(true);
area.addMouseListener(this);
// area.setFont(new Font(area.getFont().getFamily(), Font.PLAIN,
LookAndFeel.installColorsAndFont(area, "Label.background",
"Label.foreground", "TextArea.font");
area.setBorder(BorderFactory.createTitledBorder("此处可添加照片描述:"));
area.setWrapStyleWord(true);
area.getDocument().addDocumentListener(new SWING_OnValueChanged());
/*
area.setText("");
area.setText("像素大小: " + imageIcon.getIconWidth() + "*"
+ imageIcon.getIconHeight() + " 文件位置: " + picFile.toString()
area.getDocument()
.addDocumentListener(new SWING_OnValueChanged());
// area.setBackground(new Color() );
area.setFont( font);
// area.setEditable(false);
statePanel = new JPanel();
stateLabel.setText("像素大小: " + imageIcon.getIconWidth() + "*"
//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 个图像文件
imagePath = new File[imageFileNumber];
images = new Picture[imageFileNumber];
imageFileNumber = 0;
if (filePath[i] != null) {
imagePath[imageFileNumber++] = filePath[i];
imageFileNumber--;
for (int i = 0; i imagePath.length; i++) {
if (imagePath[i] == picFile) {
locationImage = i;
initTxt(imagePath[i], i);
public void initTxt(File picFile, int i) {
property.load(new FileInputStream("a.properties"));
String txt = property.getProperty(picFile.getParent() + "."
+ picFile.getName());
images[i] = new Picture(imagePath[i].getParent() + "."
+ imagePath[i].getName(), "");
//初始化图片的时候,要从资源文件里读取所有文件的txt信息,可能花费时间较长
public void Before() {
if (--locationImage 0) {
locationImage = imageFileNumber;
img = javax.imageio.ImageIO.read(imagePath[locationImage]);
imageIcon.setImage(img);
imageLabel.setIcon(imageIcon);
picFile = imagePath[locationImage];
String text = "像素大小: " + imageIcon.getIconWidth() + "*"
//System.out.println(picFile.toString());
//Picture p = new Picture(picFile, text);
Properties property = new Properties();
String areaTxt = "";
areaTxt = property.getProperty(picFile.getParent() + "."
area.setText(areaTxt);
imageScrollPane.repaint();
imageScrollPane.validate();
public void Next() {
if (++locationImage imageFileNumber) {
locationImage = 0;
public void Bigger() {
ImageIcon icon = imageIcon;
ImageIcon tmpicon = new ImageIcon(new DrawImage(icon.getImage(),
.getImage());
imageLabel.setIcon(tmpicon);
return;
public void Smaller() {
if (scale 1) {
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)) {
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区后变为文本输入指针
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
} 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) {
class SWING_OnValueChanged implements DocumentListener {
public void changedUpdate(DocumentEvent e) {
textValueChanged(e);
public void insertUpdate(DocumentEvent e) {
public void removeUpdate(DocumentEvent 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());
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");
那是一个JAVA的图片切换代码,您可以在那些网页的原代码里面找到.
一般循环的图片不超过五张.当然超过是可以的只是要一行一行去写,比较麻烦.代码就不帖了,如果没有从网站里面拷贝出来,也可以加我的QQ来找我.
以上就是土嘎嘎小编为大家整理的java图片自动播放代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!