package Viewer;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame frame = new MyFrame();
}
}
2. 界面的实现
package Viewer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
class AbstractEvent extends AbstractAction{ //set icon and string name on a button in toolbar
private String name = null;
private MyFrame frame = null;
private MyService service = MyService.getInstance(); //get the instance of service
AbstractEvent(String name, Icon icon,MyFrame frame){
super(name,icon);
this.name = name;
this.frame = frame;
}
public void actionPerformed(ActionEvent e){
//弹出确认对话框
//System.out.println(e.getActionCommand());
if (e.getActionCommand()=="Open"){
service.openFile(this.frame);
}else if (e.getActionCommand()=="Last"){
service.lastImage(this.frame);
}else if (e.getActionCommand()=="Next"){
service.nextwImage(this.frame);
}else if (e.getActionCommand()=="Bigger"){
service.zoomBigger(this.frame);
} else if (e.getActionCommand()=="Smaller"){
service.zoomSmall(this.frame);
}
}
}
public class MyFrame extends JFrame{
private int width = 1050; //define the frame's width
private int height = 700; //define the frame's height
private int pX = 40; //location of x
private int pY = 10; //location of y
private JLabel label = new JLabel(); //use a label to show a picture
private JPanel panel;
private MyService service = MyService.getInstance(); //get the instance of service
private ActionListener menuListener;
public MyFrame() //constructor function
{
menuListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
service.funcOfImage(MyFrame.this, e.getActionCommand());
}
};
initFrame();
}
void initFrame()
{
this.setTitle("Picture Viewer");
this.setLocation(pX, pY);
this.setPreferredSize(new Dimension(width, height)); //set the frame wdith and height
createMenuBar();
createToolPanel();
this.add(new JScrollPane(label), BorderLayout.CENTER);
this.setVisible(true);
this.pack();
}
void createMenuBar()
{
JMenuBar menuBar = new JMenuBar();
String menuArray[] = {"File","Func","Help"};
String menuArrayItem[][] = {
{"Open","Exit"},
{"Last","Next","Bigger","Smaller"},
{"Help"}
};
for(int i=0; i
{
JMenu menu = new JMenu(menuArray[i]);
for(int j=0; j {
JMenuItem menuItem = new JMenuItem(menuArrayItem[i][j]);
menuItem.addActionListener(menuListener);
menu.add(menuItem);
}
menuBar.add(menu);
}
this.setJMenuBar(menuBar);
}
void createToolPanel()
{
panel = new JPanel();
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(true); //实现工具栏的不浮动
String toolImgArr[] = {"Open",
"Last",
"Next",
"Bigger",
"Smaller"};
for(int i=0; i {
ImageIcon imageIcon = new ImageIcon("src/img/"+toolImgArr[i]+".gif"); //set the absolutely path
AbstractEvent absAction = new AbstractEvent(toolImgArr[i],imageIcon,this);
JButton btnTool = new JButton(absAction);
btnTool.setVerticalTextPosition(JButton.BOTTOM);
btnTool.setHorizontalTextPosition(JButton.CENTER);
toolBar.add(btnTool);
}
panel.add(toolBar);
panel.setLayout(new FlowLayout(FlowLayout.LEFT)); //set the layout flow-layout
this.add(panel,BorderLayout.NORTH);
}
public JLabel getLabel()
{
return label;
}
}
3.功能实现类
package Viewer;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyService {
private static MyService service = null;
private File currentFile = null;
private File currentDirectory = null;
private List listCurrentFile = null;
private JFileChooser fileChooser = new JFileChooser();
private double largeRate = 0.2;
public void openFile(MyFrame frame)
{
//设置文件的简单过滤
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "png","gif");
fileChooser.addChoosableFileFilter(filter);
fileChooser.showOpenDialog(frame);
fileChooser.addChoosableFileFilter(filter);
this.currentFile = fileChooser.getSelectedFile();
String fileImgName = this.currentFile.getPath(); //get current file name
//System.out.println(fileImgName);
if(fileImgName.isEmpty())
return ; //there is no choice to get
File fileDirectory = fileChooser.getCurrentDirectory();
//接下来将所有的文件
if(currentDirectory != fileDirectory)
{
File files[] = fileDirectory.listFiles();
this.listCurrentFile = new ArrayList();
for (File file : files)
{
String str = file.getName();
if(str.endsWith("png") || str.endsWith("jpg") || str.endsWith("gif"))
{
this.listCurrentFile.add(file);
// System.out.println(file);
}
}
}
ImageIcon Img = new ImageIcon(fileImgName);
frame.getLabel().setIcon(Img);
}
public void lastImage(MyFrame frame)
{
if (this.listCurrentFile != null && !this.listCurrentFile.isEmpty()) {
int index = this.listCurrentFile.indexOf(this.currentFile);
if (index > 0)
{
File file = (File) this.listCurrentFile.get(index - 1);
ImageIcon Img = new ImageIcon(file.getPath());
frame.getLabel().setIcon(Img);
this.currentFile = file;
}
else
{
return ;
}
}
else
{
return ;
}
}
public void nextwImage(MyFrame frame)
{
int lenOfList = listCurrentFile.size(); //get the size of list file
if (this.listCurrentFile != null && !this.listCurrentFile.isEmpty())
{
int index = this.listCurrentFile.indexOf(this.currentFile);
if (index+1 < lenOfList)
{
File file = (File) this.listCurrentFile.get(index + 1); //get file by index
ImageIcon Img = new ImageIcon(file.getPath());
frame.getLabel().setIcon(Img);
this.currentFile = file;
}
else
{
return ;
}
}
else
{
return ;
}
}
public void zoomBigger(MyFrame frame)
{
ImageIcon Img = (ImageIcon) frame.getLabel().getIcon();
if (Img != null)
{
int width = (int) (Img.getIconWidth() * (1+largeRate));
ImageIcon newImg = new ImageIcon(Img.getImage().getScaledInstance(width, -1, Image.SCALE_DEFAULT));
frame.getLabel().setIcon(newImg);
}
else
{
return ;
}
}
public void zoomSmall(MyFrame frame)
{
ImageIcon Img = (ImageIcon) frame.getLabel().getIcon();
//System.out.println("**************");
if (Img != null)
{
int width = (int) (Img.getIconWidth() * (1-largeRate));
ImageIcon newImg = new ImageIcon(Img.getImage().getScaledInstance(width, -1, Image.SCALE_DEFAULT));
frame.getLabel().setIcon(newImg);
}
else
{
return ;
}
}
/*
* 获取当前的句柄
*/
public static MyService getInstance() {
if (service == null)
{
service = new MyService();
}
return service;
}
public void funcOfImage(MyFrame frame, String cmd)
{
if(cmd.equals("Open")){
openFile(frame);
}else if(cmd.equals("Exit")){
System.exit(0);
}else if(cmd.equals("Last")){
lastImage(frame);
}else if(cmd.equals("Next")){
nextwImage(frame);
}else if(cmd.equals("Bigger")){
zoomBigger(frame);
}else if (cmd.equals("Smaller")){
zoomSmall(frame);
}else
return;
}
}
唉,,图片上传不了。。。。。
QQ 交流群: 204944806