java图像界面开发简单实例-ImageIO、JFileChooser、JMenu、JPanel的应用

java图像界面开发简单实例

ImageIO,JFileChooser,JMenu,JPanel的应用,JLabel,JFileChooser,JMenu应用例子中图片是通过设置JLabel的Icon属性来实现图片浏览的,本例则通过JPanel,利用ImageIO来读取相应文件,显示图片,代码如下:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
*ImageIO的简单应用实例
*
@author 左杰jdk5.0
*
*/
public class Example7Frame extends JFrame{

private static final long serialVersionUID = 1L ;
private JLabellabel;
private JFileChooserfileChooser;
private ImagePanelpanel;
public Example7Frame(){
setTitle(
" 图片浏览 " ); // 设置窗体标题
setSize( 500 , 400 ); // 设置窗体大小
// 创建标签对象
label = new JLabel();
add(label);
// 在窗体上添加标签
// 创建文件选择器对象
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(
new File( " . " )); // 设置默认路径为当前目录
// 创建菜单栏
JMenuBarmenuBar = new JMenuBar();
setJMenuBar(menuBar);
// 在窗体上添加菜单栏
// 添加菜单项
JMenumenu = new JMenu( " 文件 " );
menuBar.add(menu);
// 在菜单栏中添加菜单项
// 添加“打开”子菜单项
JMenuItemopenItem = new JMenuItem( " 打开 " );
menu.add(openItem);
// 在菜单项中添加子菜单项
panel = new ImagePanel(); // 创建显示图片面板
add(panel); // 窗体中添加图片面板
// 为“打开”菜单添加事件及监听
openItem.addActionListener( new ActionListener(){
public void actionPerformed(ActionEventevent){
// 显示文件选择器
int result = fileChooser.showOpenDialog( null );
// 如果选择文件则显示在标签中
if (result == JFileChooser.APPROVE_OPTION){
Stringname
= fileChooser.getSelectedFile().getPath(); // 获取选择文件的路径
panel.setImage(name); // 设置图片路径
panel.repaint(); // 更新面板
}
}
});
// 添加“退出”子菜单项
JMenuItemexitItem = new JMenuItem( " 退出 " );
menu.add(exitItem);
// 在菜单项中添加子菜单项
// 为“退出”菜单添加事件及监听
exitItem.addActionListener( new ActionListener(){
public void actionPerformed(ActionEventevent){
System.exit(
0 );
}
});
}

public static void main(String[]args){
Example7Frameframe
= new Example7Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(
true );
}
}

/**
*创建用于显示图片的面板
*/
class ImagePanel extends JPanel{

private static final long serialVersionUID = 1L ;
private Imageimage;
private int showWdith;
private int showHeight;
public void setImage(StringfileName){
// 读取图片文件
try {
image
= ImageIO.read( new File(fileName));
}
catch (IOExceptione){
e.printStackTrace();
}
}

public void paintComponent(Graphicsg){
super .paintComponent(g);
if (image == null )
return ;
// 获取图像文件的宽度和高度
int imageWidth = image.getWidth( this );
int imageHeight = image.getHeight( this );
// 获取面板的高度和宽度
int width = getWidth();
int height = getHeight();
// 图片最大显示为面板的大小
if (imageWidth > width){
this .showWdith = width;
}
else {
this .showWdith = imageWidth;
}
if (imageHeight > height){
this .showHeight = height;
}
else {
this .showHeight = imageHeight;
}
g.drawImage(image,
0 , 0 ,showWdith,showHeight, null , null ); // 在面板上绘制图片内容
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值