加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

简单的 java图片浏览器示例

发布时间:2020-12-14 23:51:03 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.CropImageFilter;import java.a

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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();
    }

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读