请给我的JAVA程序添加一个自动播放图片功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请给我的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,呵呵,大大们看怎么设计啦,最符合要求的加分,能加多少加多少
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)
参考技术A 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();
参考技术B 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();
参考技术C 晚上回去了给你写 参考技术D 做的效果 就是每隔几次 换一张 这样是么?
是否可以在命令行/作为 web 服务中运行 java 应用程序
【中文标题】是否可以在命令行/作为 web 服务中运行 java 应用程序【英文标题】:Is it possible to run java application in both commandline / as a webservice 【发布时间】:2018-11-29 15:51:18 【问题描述】:我想创建一个类似于比特币客户端的 Java 应用程序。但它可以在命令行 cmd 上运行,也可以作为 Web 服务向其他客户咨询我的客户。我应该使用哪种技术来处理我的任务。请给我一个例子解释问题
【问题讨论】:
你也可以在包中添加一个类,它有一个可运行的 Main - 调用你的 web 服务,我在测试 web 服务时经常这样做 【参考方案1】:期望
猜猜这就是你所期待的
-
从命令行或作为操作系统服务运行 Java 程序
该程序应该是向其他消费者公开一些 API 的 Web 服务
解决方案
您可以在 Spring MVC、Spring Boot 和 Maven 的帮助下做到这一点原因
Spring MVC 可用于将您的业务公开为 REST Web 服务 Spring Boot 可用于将 Web 服务应用程序作为普通 Java 应用程序(使用主类)启动,而无需使用 Tomcat 或 Jetty 等外部 Web 服务器 Maven 构建工具可用于从 Spring Boot 应用程序创建 fat jar(包含所有依赖项的单个 jar 文件) 您将从命令行运行生成的 fat jar,但服务仍将作为 REST 服务公开参考
使用https://start.spring.io/ 这个 Spring 自己的托管工具将为您生成所有样板代码。您只需将下载的 zip 导入您的工作区并添加您的业务逻辑 要构建一个胖罐子,您不需要做任何特别的事情。只需运行mvn clean install
。样板将有一个 pom.xml 文件,该文件将负责生成 fat jar
【讨论】:
非常感谢您详细、优雅的回答。你的最后一句话引起了我的注意。我将把这个程序提供给我的股票经纪人客户,他们可以使用这个程序访问我系统中的信息。在这样做时,它将使用命令行或浏览器。这部小说有错误吗。不过,我可以用你所说的解决它。 我从来没有使用过spring boot和maven。有没有一个例子涵盖了你提到的完成这项任务的技术。 编辑了答案,希望能帮助你快速启动(也许这就是spring boot的动机)。 这样,我可以同时从命令行和浏览器发出请求吗? 当你的意思是请求时,我猜你的意思是 HTTP 请求。是的,您可以从浏览器和 cmd 中获取。对于 cmd 你必须使用 cURL 命令。以上是关于请给我的JAVA程序添加一个自动播放图片功能的主要内容,如果未能解决你的问题,请参考以下文章