java 简单图片浏览器_简单的 java图片浏览器示例

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.CropImageFilter;

import java.awt.image.FilteredImageSource;

import java.awt.image.ImageFilter;

import java.awt.image.ImageProducer;

import java.io.File;

import javax.swing.*;

public class vicePic implements ActionListener {

JLabel imgLable;

JFrame mainJframe;

Container con;

JTextField fileFiled;

JButton openBtn,cutBtn;

JPanel pane;

JScrollPane spanel;

ImageIcon img;

public vicePic(){

mainJframe=new JFrame("图形显示比例");

con=mainJframe.getContentPane();

pane=new JPanel();

pane.setLayout(new FlowLayout());

openBtn=new JButton("打开文件");

openBtn.addActionListener(this);

cutBtn=new JButton("剪切图片");

cutBtn.addActionListener(this);

fileFiled=new JTextField();

fileFiled.setColumns(20);

pane.add(fileFiled);

pane.add(openBtn);

pane.add(cutBtn);

imgLable =new JLabel();

spanel=new JScrollPane(imgLable);

con.add(pane,BorderLayout.NORTH);

con.add(spanel,BorderLayout.CENTER);

mainJframe.setSize(800, 800);

mainJframe.setVisible(true);

mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override

public void actionPerformed(ActionEvent e) {

String cmd=e.getActionCommand();

ImageFilter cropFilter;

Image croppedImage;

ImageProducer produer;

if(cmd.equals("打开文件")){

try{

JFileChooser chooser=new JFileChooser();

if(chooser.showOpenDialog(mainJframe)==JFileChooser.APPROVE_OPTION){

File tempfile=chooser.getSelectedFile();

fileFiled.setText(tempfile.toString());

img=new ImageIcon(fileFiled.getText());

imgLable.setIcon(img);

}http://www.huiyi8.com/jiaoben/

}catch(Exception el){

JOptionPane.showMessageDialog(mainJframe, "图品无法显示");

}

}

if(cmd.equals("剪切图片")){

cropFilter=new CropImageFilter(100, 100, 200, 200);

produer=new FilteredImageSource(img.getImage().getSource(), cropFilter);

croppedImage=Toolkit.getDefaultToolkit().createImage(produer);

imgLable.setIcon(new ImageIcon(croppedImage));

}

}

public static void main(String[] args){

new vicePic();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值