2014.11.18作业,有借鉴部分

本文介绍了一个使用Java Swing实现的简单图片浏览显示窗口程序。该程序允许用户通过按钮选择本地图片,并将其显示在一个JFrame窗口中。代码展示了如何创建GUI界面、设置文件选择器以及加载并显示图片。

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

<pre name="code" class="java">package com.hechao;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileFilter;
/**
 * 图片浏览显示窗口
 * @author hechao
 *
 */
@SuppressWarnings("serial")
public class MyFrame extends JFrame{
	private JButton scanButton;
	private JFileChooser chooser;
	private JLabel pictrueLable = null;
	private String imageName = null;
	
	public MyFrame(){
		this.setTitle("图片显示窗口");
		this.setSize(600, 700);
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		pictrueLable = new JLabel("", JLabel.CENTER);
		pictrueLable.setBounds(40, 40, 500, 550);
		this.add(pictrueLable);
		
		chooser = new JFileChooser();				
		chooser.setFileFilter(new FileFilter(){
			
			@Override
			public String getDescription() {
				return "图片";
			}

			@Override
			public boolean accept(File f) {
				return true;
			}
		});
		scanButton = new JButton("浏览");
		scanButton.setBounds(270, 630, 60, 30);
		scanButton.addActionListener(new ActionListener() {
			
			@SuppressWarnings("static-access")
			@Override
			public void actionPerformed(ActionEvent e) {
				int i = chooser.showOpenDialog(null);
				if(i == chooser.APPROVE_OPTION){
					File f = chooser.getSelectedFile();
					 imageName = f.getAbsolutePath();
				}
				pictrueLable.setIcon(new ImageIcon(imageName));
			}
		});
		this.add(scanButton);
	}
	
	public static void main(String[] args) {
		new MyFrame().setVisible(true);
	}
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值