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

本文介绍了一个使用Java实现的图像界面开发示例,通过ImageIO读取并显示图片,结合JFileChooser进行文件选择,利用JPanel展示图像,并集成了JLabel、JMenu等组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 左杰  jdk 5.0
 *
 
*/
public class Example7Frame extends JFrame {

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

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

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

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

    
public void paintComponent(Graphics g) {
        
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, 
00, showWdith, showHeight, nullnull);//在面板上绘制图片内容
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jiqimiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值