
正文
java图片浏览器代码 java实现浏览器
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求一个java图片浏览器的源代码,拜托大家了!!!
给你代码java图片浏览器代码,看一下注释,为是防止左右移动时鼠标是水平java图片浏览器代码的移动,所以加了一个范轩,
在二十个象素内,认为是水平的。
你看一下吧
还好你这个是鼠标按住移动,如果只是鼠移动就麻烦了
----------------------------------------
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);
if ((y - thisY 20 y - thisY 0)
|| (y - thisY 0 y - thisY -20)) {
// Y 在20范围内移动认为是水平移动
if (x thisX) {
// right
if (selectedFiles != null
fileIndex selectedFiles.length - 1) {
fileIndex++;
}
} else {
// left
if (selectedFiles != null fileIndex 0) {
fileIndex--;
}
}
} else {
if (x thisX) {
// 右下
width += 20;
height += 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}
相关问答
Q1: 请教java高手们,帮我写一个java编写的图片浏览器,功能如下:有自动浏览功能,每隔几秒图片自动翻页。用
//改编java图片浏览器代码的java图片浏览器代码,CopyOfImageViewer.java 打开一个有图片的文件夹就可浏览java图片浏览器代码了。
//MP3播放相关库到:;nbsp;下载
//将下载到的zip文件里的 jl1.0.1.jar 复制到 JDK目录下的 jre/lib/ext/ 目录里即可.
//将 源代码 main 方法里的 playMp3("d:\\bad.mp3");改成自己的地址java图片浏览器代码,换种方法BMP是可以支持的,这里不行暂不讨论。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javazoom.jl.player.Player;
public class CopyOfImageViewer implements ActionListener,Runnable {
JPanel bts;
JLabel pl;
JScrollPane jsp;
JButton cf,start,next,prev,stop;
JFrame f;
JFileChooser fc;
File [] sf;
int index;
Thread auto;
boolean autoFlag;
int delay=5*1000;
//这里就是GUI布局
CopyOfImageViewer(){
pl=new JLabel();
pl.setHorizontalAlignment(JLabel.CENTER);
jsp=new JScrollPane(pl);
start=new JButton("start");
next=new JButton("");
prev=new JButton("");
stop=new JButton("stop");
bts=new JPanel(new FlowLayout(FlowLayout.CENTER));
bts.add(start);
bts.add(prev);
bts.add(next);
bts.add(stop);
cf=new JButton("Select a picture folder");
fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f=new JFrame();
f.setDefaultCloseOperation(3);
f.getContentPane().add(cf,"North");
f.getContentPane().add(jsp,"Center");
f.getContentPane().add(bts,"South");
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setVisible(true);
//给按钮加入事件侦听器
start.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
stop.addActionListener(this);
cf.addActionListener(this);
auto=new Thread(this);
auto.start();
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exception e){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e2){}
}
new CopyOfImageViewer();
playMp3("d:\\bad.mp3");
}
//简单MP3播放
private static void playMp3(String file){
try{
Player p = new Player(new FileInputStream(file));
p.play();
}catch(Exception e){}
}
//处理各按键事件
public void actionPerformed(ActionEvent e) {
Object src=e.getSource();
if(src==cf){
int o=fc.showOpenDialog(f);
if(o==JFileChooser.APPROVE_OPTION){
sf=fc.getSelectedFile().listFiles(new FilenameFilter(){
//合法的文件后缀
String[] suf={".PNG",".GIF",".JPG",};
public boolean accept(File dir, String name) {
name=name.toUpperCase();
for(int i=0; isuf.length; i++)
if(name.endsWith(suf[i]))return true;
return false;
}
});
if(sf.length0){
index=0;
showPic();
}
}
}
if(sf==null||sf.length==0)return;
if(src==start)startB();
else if(src==stop)stopB();
else if(src==next)next();
else if(src==prev)prev();
}
void prev(){
index=--index0?sf.length-1:index;
showPic();
}
void next(){
index=++indexsf.length-1?0:index;
showPic();
}
public void run(){
while(true){
if(sf!=null sf.length0 autoFlag){
try {Thread.sleep(delay);} catch (Exception e) {}
next();
}
try {Thread.sleep(100);} catch (Exception e) {}
}
}
private void stopB() {
autoFlag=false;
}
private void startB() {
autoFlag=true;
}
//显示图片
private void showPic() {
if(sf==null || sf.length==0)return;
pl.setIcon(new ImageIcon(sf[index].getAbsolutePath()));
System.out.println(sf[index].getAbsolutePath());
}
}
Q2: java swing编写的图片浏览器不显示图片,这是文件选择器的代码
关于样式显示不出来: 1.检查样式的书写格式是否正确,如:style/style可能无效,尝试style type="text/css"/style 另外,检查样式代码中是否缺少结束标识符。 2.如果是引用的外部样式文件,检查下路径是否正确! 3.部分样式无效,检查选择器是否正确,如样式定义为:#box,检查标签是否为:div id="box"/div,防止class和id混淆。 4.动态页面有时候需要多刷新几下更新缓存。多刷新几下确保样式被重新加载。 5.样式兼容性,这个就比较特殊了,检查你的样式整体设计是否合理。有时候浮动、高度等设置不当,导致页面无法正常显示,另外,标签或属性的书写错误也会导致样式执行失效。
Q3: 用java编写一个图片浏览器
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class ImageGallery extends JFrame implements ActionListener {
public static final int G_WIDTH = 400, G_HEIGHT = 620;
public static final int L_WIDTH = 1200, L_HEIGHT = 520;
public static final int MAX_R = 3, MAX_C = 2;
public static final int GRID = 1, LARGE = 2;
private JFileChooser chooser;
private JMenuBar toolBar;
private JMenu menu;
private JMenuItem open;
private ArrayListString paths;
private JPanel showingPanel, buttonPanel;
private int page = 1;
private int count = 0;
private int showType = GRID;
private int pageMax;
private JButton last, next, large, grid;
public ImageGallery() {
this.setTitle("Image gallery 0.1");
this.setBounds(100, 100, G_WIDTH, G_HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
toolBar = new JMenuBar();
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
menu = new JMenu("File");
toolBar.add(menu);
open = new JMenuItem("open");
open.addActionListener(this);
menu.add(open);
paths = new ArrayListString();
this.setJMenuBar(toolBar);
showingPanel = new JPanel();
buttonPanel = new JPanel();
last = new JButton("last");
last.addActionListener(this);
next = new JButton("next");
next.addActionListener(this);
large = new JButton("large");
large.addActionListener(this);
grid = new JButton("grid");
grid.addActionListener(this);
buttonPanel.add(last);
buttonPanel.add(large);
buttonPanel.add(grid);
buttonPanel.add(next);
this.add(buttonPanel, BorderLayout.SOUTH);
this.add(showingPanel);
this.setVisible(true);
}
public void doOpen() {
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
String directory = chooser.getSelectedFile().getPath();
this.getAllImage(directory);
for (String s : this.paths) {
System.out.println(s);
}
this.showingImage();
}
}
public void getAllImage(String directory) {
paths.clear();
page = 1;
count = 0;
File file = new File(directory);
if (file.isDirectory()) {
String[] list = file.list();
for (String s : list) {
String path = directory + "\\" + s;
File temp = new File(path);
if (!temp.isDirectory()) {
this.paths.add(path);
}
}
}
}
public void changePage(int page) {
if (!paths.isEmpty()) {
switch (showType) {
case GRID:
this.page += page;
if (this.page == 0) {
this.page = 1;
}
if (this.page pageMax) {
this.page = pageMax;
}
break;
case LARGE:
this.count += page;
if (count 0) {
count = 0;
}
if (count paths.size() - 1) {
count = paths.size() - 1;
}
break;
}
this.showingImage();
}
}
public void showingImage() {
this.remove(showingPanel);
showingPanel = new JPanel();
int size = paths.size();
int max = MAX_R * MAX_C;
pageMax = (size % max == 0 ? size / max : size / max + 1);
switch (showType) {
case GRID:
this.setBounds(100, 100, G_WIDTH, G_HEIGHT);
showingPanel.setLayout(new GridLayout(MAX_R, MAX_C - 1));
int i = (page - 1) * max;
int j = page * max;
for (; i j i size; i++) {
String path = paths.get(i);
showingPanel.add(new ImagePanel(path));
}
break;
case LARGE:
int left = count - 1;
int right = count + 1;
this.setBounds(100, 100, L_WIDTH, L_HEIGHT);
showingPanel.setLayout(new GridLayout(1, 2));
if (left = 0) {
showingPanel.add(new ImagePanel(paths.get(left)));
} else {
showingPanel.add(new ImagePanel(""));
}
showingPanel.add(new ImagePanel(paths.get(count)));
if (right size) {
showingPanel.add(new ImagePanel(paths.get(right)));
} else {
showingPanel.add(new ImagePanel(""));
}
break;
}
this.add(showingPanel, BorderLayout.CENTER);
showingPanel.updateUI();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new ImageGallery();
}
class ImagePanel extends JPanel {
private ImageIcon image;
ImagePanel(String path) {
image = new ImageIcon(path);
}
public ImageIcon getImageIcon() {
return this.image;
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(image.getImage(), 0, 0, this.getWidth(), this
.getHeight(), this);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String key = e.getActionCommand();
System.out.println(key);
if (key.equals("open")) {
doOpen();
} else if (key.endsWith("last")) {
changePage(-1);
} else if (key.endsWith("next")) {
changePage(1);
} else if (key.endsWith("large")) {
if (!paths.isEmpty()) {
count =0;
showType = LARGE;
showingImage();
}
} else if (key.endsWith("grid")) {
if (!paths.isEmpty()) {
page = 1;
showType = GRID;
showingImage();
}
}
}
}
Q4: 简单的java图片浏览器,使图片居中显示
有两种方法:
1. 将图片缩放,也就是显示缩略图
2. 使用JScrollPane,显示滚动条
第一种比较复杂,这里给出第二种方法的实现:
将你代码的第17、18行改成:
label = new JLabel();
JScrollPane scroll = new JScrollPane(label);
add(scroll);
Q5: 10R,求个简单的JAVA图片浏览器,窗体我设计好了,只需要,下一张,上一张按钮具有功能。,急
自定义一个jpanel用于展示image
/**
*
*/
package org.demo;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;
/**
* @ClassName: ImageView
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Btboy1978
* @date 2017年6月12日 下午9:46:13 *
*/
public class ImageView extends JPanel {
private MapInteger, Image images;
private int currentIndex;
public ImageView() {
super();
images = new HashMapInteger, Image();
currentIndex = 0;
}
/*
* (non-Javadoc)
*
* @see javax.swing.JComponent#paint(java.awt.Graphics)
*/
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(images.get(currentIndex), 0, 0, this);
}
public void showNext() {
if (images.size() != 0) {
if (currentIndex != images.size() - 1) {
currentIndex++;
}
}
this.repaint();
}
public void showUp() {
if (images.size() != 0) {
if (currentIndex != 0) {
currentIndex--;
}
}
this.repaint();
}
public void addImage(Image image){
images.put(images.size(), image);
System.out.println(images.size());
}
}
测试方法
/**
*
*/
package org.demo;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;
/**
* @ClassName: ImageViewTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Btboy1978
* @date 2017年6月12日 下午9:59:42 *
*/
public class ImageViewTest extends JFrame {
private JPanel contentPane;
private ImageView imageView;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ImageViewTest frame = new ImageViewTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ImageViewTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 393);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
imageView = new ImageView();
imageView.setBounds(10, 10, 414, 284);
try {
imageView.addImage(ImageIO.read(new File("images/1.jpg")));
imageView.addImage(ImageIO.read(new File("images/2.jpg")));
imageView.addImage(ImageIO.read(new File("images/3.jpg")));
imageView.addImage(ImageIO.read(new File("images/4.jpg")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
contentPane.add(imageView);
JButton btnNewButton = new JButton("UP");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imageView.showUp();
}
});
btnNewButton.setBounds(81, 304, 117, 41);
contentPane.add(btnNewButton);
JButton button = new JButton("NEXT");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
imageView.showNext();
}
});
button.setBounds(228, 304, 117, 41);
contentPane.add(button);
}
}
java图片浏览器代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现浏览器、java图片浏览器代码的信息别忘了在本站进行查找喔。






