📄 imagesbrowse.java
字号:
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.*;//用来打开对话框里指定文件后缀名
import java.awt.image.*;
import javax.imageio.*;
import javax.imageio.ImageIO;
public class ImagesBrowse extends JFrame implements ActionListener{
private JMenuItem jmiOpen = new JMenuItem("打开(O)");
private JMenuItem jmiSave = new JMenuItem("保存(S)");
private JMenuItem jmiSaveAs = new JMenuItem("另存为(S)");
private JMenuItem jmiExit = new JMenuItem("退出(E)");
private JMenuItem jmiPrevious = new JMenuItem("上一张(P)");
private JMenuItem jmiNext = new JMenuItem("下一张(N)");
private JMenuItem jmiZoomOut = new JMenuItem("放大(O)");
private JMenuItem jmiZoomIn = new JMenuItem("缩小(I)");
private JMenuItem jmiInfo = new JMenuItem("说明(I)");
private JMenuItem jmiAbout = new JMenuItem("关于(A)");
private JButton jbtPrevious = new JButton("上一张");
private JButton jbtNext = new JButton("下一张");
private JButton jbtZoomOut = new JButton("放大");
private JButton jbtZoomIn = new JButton("缩小");
private JButton jbtReset = new JButton("还原");
private JFileChooser jfc = new JFileChooser(new File("."));
private JLabel jl = new JLabel();
private JLabel status = new JLabel("信息提示");
public ImagesBrowse(){
JMenu jm1 = new JMenu("文件(F)");
jm1.add(jmiOpen);
jm1.add(jmiSave);
jm1.add(jmiSaveAs);
jm1.addSeparator();
jm1.add(jmiExit);
JMenu jm2 = new JMenu("编辑(E)");
jm2.add(jmiPrevious);
jm2.add(jmiNext);
jm2.add(jmiZoomOut);
jm2.add(jmiZoomIn);
JMenu jm3 = new JMenu("帮助(H)");
jm3.add(jmiInfo);
jm3.add(jmiAbout);
JMenuBar jmBar = new JMenuBar();
jmBar.add(jm1);
jmBar.add(jm2);
jmBar.add(jm3);
setJMenuBar(jmBar);
JPanel jp1 = new JPanel();
jp1.add(jbtPrevious);
jp1.add(jbtZoomOut);
jp1.add(jbtReset);
jp1.add(jbtZoomIn);
jp1.add(jbtNext);
JPanel jp2 = new JPanel(new BorderLayout()); //JPanel的默认布局是FlowLayout
jp2.add(new JScrollPane(jl),BorderLayout.CENTER); //最好不要设置为NORTH,否则jp1的按钮会隐藏
// jl.setOpaque(true); //JLabel默认是透明的,所以直接setBackground()不能设置其背景颜色,但setForceground不用
// jl.setBackground(Color.white);
jl.setHorizontalAlignment(0); //将jl设置放在中央,不加这句,图片/文字将显示水平靠左,垂直为中间的位置,
//我也百思不得其解.括号里的数是从0-1的int,alignment的定义
jp2.add(jp1,BorderLayout.SOUTH);//将jp1的按钮添加进jp2里
getContentPane().add(jp2,BorderLayout.CENTER);
getContentPane().add(status,BorderLayout.SOUTH);
jmiOpen.addActionListener(this);//按钮事件
jmiSave.addActionListener(this);
jmiSaveAs.addActionListener(this);
jmiExit.addActionListener(this);
jmiInfo.addActionListener(this);
jmiAbout.addActionListener(this);
jbtPrevious.addActionListener(this);
jbtNext.addActionListener(this);
jbtZoomOut.addActionListener(this);
jbtReset.addActionListener(this);
jbtZoomIn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == jmiOpen)
open();
else if(e.getSource() == jmiSave)
save();
else if(e.getSource() == jmiSaveAs)
saveAs();
else if(e.getSource() == jmiExit)
System.exit(0);
else if(e.getSource() == jbtPrevious)
previous();
else if(e.getSource() == jbtNext)
next();
else if(e.getSource() == jbtZoomOut)
zoomOut();
else if(e.getSource() == jbtReset)
reset();
else if(e.getSource() == jbtZoomIn)
zoomIn();
else if(e.getSource() == jmiInfo){
JOptionPane.showMessageDialog(null,"此程序只能打开下列格式的图像:\n.jpg.bmp.png.gif",
"说明",JOptionPane.INFORMATION_MESSAGE);
}
else if(e.getSource() == jmiAbout){
JOptionPane.showMessageDialog(null,"此程序由牛牛编写.\n2008年11月",
"关于",JOptionPane.INFORMATION_MESSAGE);
}
}
public void open(){
jfc.resetChoosableFileFilters();//将可选择文件过滤器列表重置为其开始状态,不设置的结果会很烦
FileNameExtensionFilter filter1 = new FileNameExtensionFilter("PNG Images","png");//设置打开指定后缀名的文件
FileNameExtensionFilter filter2 = new FileNameExtensionFilter("GIF Images","gif");
FileNameExtensionFilter filter3 = new FileNameExtensionFilter("BMP Images","bmp");
FileNameExtensionFilter filter4 = new FileNameExtensionFilter("JPG Images","jpg");
jfc.setFileFilter(filter1);
jfc.setFileFilter(filter2);
jfc.setFileFilter(filter3);
jfc.setFileFilter(filter4);
if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
try{
BufferedImage bi = ImageIO.read(jfc.getSelectedFile()); //Java默认支持的图片类型为jpg,bmp,gif,png
ImageIcon icon = new ImageIcon(bi);
jl.setIcon(icon); // void setIcon(Icon icon) 定义此组件将要显示的图标,API里的JLabel
long length = jfc.getSelectedFile().length(); //图像文件的大小,在file里定义了此方法
int width = bi.getWidth(); //图像的高度
int height = bi.getHeight(); //图像的宽度,在JDK API里的BufferedImage有定义,或用ImageIcon里的方法:
// icon.getIconWidth()和icon.getIconHeight().
status.setText(" " + jfc.getSelectedFile().getPath() + " 文件已打开;" +
" 大小: " + length + " bytes;" +
" 分辨率: " + width + " × " + height);
}
catch(IOException ex){
JOptionPane.showMessageDialog(null,"The image is not supported!",
"Error",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public void save(){
try{
Icon icon = jl.getIcon(); //从jl中得到图片
int width = icon.getIconWidth();
int height = icon.getIconHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//把TYPE_INT_RGB改成TYPE_INT_ARGB图像会变得模糊
Graphics g = image.getGraphics();
icon.paintIcon(null, g, 0, 0);
File output = new File(jfc.getSelectedFile().getPath());//保存替换原图像
int info = JOptionPane.showConfirmDialog(null,"是否真的要替换原图像?","提示:",JOptionPane.OK_CANCEL_OPTION,2);
if(info == JOptionPane.OK_OPTION)
ImageIO.write(image,"jpg",output);//把image写入output中
else
return;
status.setText(" " + jfc.getSelectedFile().getPath() + " 文件已保存");
}
catch(IOException ex){
}
}
public void saveAs(){
jfc.resetChoosableFileFilters();//将可选择文件过滤器列表重置为其开始状态
FileNameExtensionFilter filter1 = new FileNameExtensionFilter("PNG Images","png");
FileNameExtensionFilter filter2 = new FileNameExtensionFilter("GIF Images","gif");
FileNameExtensionFilter filter3 = new FileNameExtensionFilter("BMP Images","bmp");
FileNameExtensionFilter filter4 = new FileNameExtensionFilter("JPG Images","jpg");
jfc.setFileFilter(filter1);
jfc.setFileFilter(filter2);
jfc.setFileFilter(filter3);
jfc.setFileFilter(filter4);
if(jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
try{
Icon icon = jl.getIcon(); //从jl中得到图片
int width = icon.getIconWidth();
int height = icon.getIconHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//存储后变小了,jdk6里的帮助文件的说明是:
//TYPE_INT_RGB:表示一个图像,它具有合成整数像素的 8 位 RGB 颜色分量。该图像具有不带 alpha 的 DirectColorModel。
//当具有透明 alpha 的数据存储在此类型的图像中时,必须将颜色数据调整为非预乘形式并丢弃 alpha,如 AlphaComposite 文档中的描述。
Graphics g = image.getGraphics();
icon.paintIcon(null, g, 0, 0);
String str = null;
if(jfc.getFileFilter() == filter1) //保存时选择格式
str = "png";
else if(jfc.getFileFilter() == filter2)
str = "gif";
else if(jfc.getFileFilter() == filter3)
str = "bmp";
else if(jfc.getFileFilter() == filter4)
str = "jpg";
String name = "." + str;
File output = null;
output = new File(jfc.getSelectedFile().getPath().concat(name));
status.setText(" " + jfc.getSelectedFile().getPath() + name + " 文件已保存");
// }
//其他1. String name = jfc.getName(jfc.getSelectedFile()); //获取jfc对话框中的文本框输入的文件名,jfc.getSelectedFile()属于File类型
// File output = new File(name + "." + str); //指定后缀和输入任意文件名存储在原文件路径中
// ImageIO.write(image,str,output);
//其他2. File output = new File(jfc.getSelectedFile().getPath()); //指定路径,但存储时手动加上后缀名
// ImageIO.write(image,str,output);
if(output.exists()){
int info = JOptionPane.showConfirmDialog(null,"文件已经存在,是否要覆盖原文件?","提示:",JOptionPane.OK_CANCEL_OPTION,2);
if(info == JOptionPane.OK_OPTION)
ImageIO.write(image,str,output); //以str格式编码把image写入output中,没有这句的话是无法输出图像文件的
else
return;
}
else
ImageIO.write(image,str,output);
}
catch(IOException ex){
}
}
}
public void zoomOut(){
/** Icon icon = jl.getIcon(); //从jl中得到图片
int width = icon.getIconWidth();
int height = icon.getIconHeight();
Image image = this.getToolkit().getImage(jfc.getSelectedFile().getPath());
Image zoomImage=image.getScaledInstance(width*2,height*2,Image.SCALE_DEFAULT);
jl.setIcon((Icon)zoomImage);
long length = jfc.getSelectedFile().length();
status.setText(" " + jfc.getSelectedFile().getPath() + " 文件已放大;" +
" 大小: " + length + " bytes" +
" 分辨率: " + width + " × " + height);
*/
}
public void zoomIn(){
}
public void reset(){
}
public void previous(){
}
public void next(){
}
public static void main(String[] args){
ImagesBrowse images = new ImagesBrowse();
images.setTitle("图像浏览器");
images.setVisible(true);
images.setSize(1000,800);
images.setLocation(110,110);
images.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -