欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

图片查看器

程序员文章站 2023-04-04 12:18:08
1.下面一段代码,不知道是怎么打开图片文件的。 运行后打开图片: ......

1.下面一段代码,不知道是怎么打开图片文件的。

/**
 * @author steven
 * func: 查看照片
 * created : 2019-8-17
 */

import java.awt.eventqueue;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.keyevent;
import java.io.file;
import javax.swing.imageicon;
import javax.swing.jdialog;
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.keystroke;
import javax.swing.swingconstants;

public class imageviewerdemo {
    public static void main(string[] args) {
        eventqueue.invokelater(()->{
            jframe frame = new imageviewerframe();
            frame.setvisible(true);
        });
    }
}

class imageviewerframe extends jframe{
    private static final long serialversionuid = 1l;
    private jlabel label;
    private jfilechooser chooser;
    private static final int default_width = 800;
    private static final int default_height = 600;
    
    //无参构造
    public imageviewerframe() {
        settitle("图片查看器");
        setdefaultcloseoperation(jframe.exit_on_close);
        
        //定义标签
        setbounds(300,100,default_width, default_height);
        label = new jlabel();
        add(label);
        
        //定义文件选择器
        chooser = new jfilechooser();
        chooser.setcurrentdirectory(new file("."));
        
        //定义菜单栏
        jmenubar menubar = new jmenubar();
        setjmenubar(menubar);
        
        //定义"文件"菜单
        jmenu filemenu = new jmenu("文件(f)");    
        filemenu.setmnemonic(keyevent.vk_f);    //文件快捷键:alt + f
        menubar.add(filemenu);
        
        //定义"帮助"菜单
        jmenu helpmenu = new jmenu("帮助(h)");
        filemenu.setmnemonic(keyevent.vk_h);    //帮助快捷键: alt + h
        menubar.add(helpmenu);
        
        //定义"帮助"菜单项
        jmenuitem aboutitem = new jmenuitem("关于(a)");
        aboutitem.setaccelerator(keystroke.getkeystroke(keyevent.vk_a, keyevent.alt_down_mask));    //关于加速键:alt + a
        helpmenu.add(aboutitem);
        aboutitem.addactionlistener(new actionlistener() {
            @override
            public void actionperformed(actionevent e) {
                jdialog aboutdialog = new jdialog();
                aboutdialog.settitle("关于" + (new imageviewerframe()).gettitle());
                aboutdialog.setresizable(false);
                aboutdialog.setbounds(600, 300, 200, 100);
                
                jlabel aboutcontentlabel = new jlabel();
                aboutcontentlabel.settext("<html><body>图片查看器<br>作者:steven<br>邮箱:99388239@qq.com</body></html>");
                aboutcontentlabel.sethorizontalalignment(swingconstants.center);
                
                aboutdialog.add(aboutcontentlabel);
                aboutdialog.setvisible(true);
            }
        });

        //定义"打开"菜单项
        jmenuitem openitem = new jmenuitem("打开(o)");
        openitem.setaccelerator(keystroke.getkeystroke(keyevent.vk_o, keyevent.alt_down_mask));//打开加速键:alt + o
        filemenu.add(openitem);
        openitem.addactionlistener(event -> {
            int result = chooser.showopendialog(null);
            if(result == jfilechooser.approve_option) {
                string name = chooser.getselectedfile().getpath();
                label.seticon(new imageicon(name));
            }
        });
        
        //定义"退出"菜单项
        jmenuitem exititem = new jmenuitem("退出(q)");
        exititem.setaccelerator(keystroke.getkeystroke(keyevent.vk_q, keyevent.alt_down_mask)); //退出加速键:alt + q
        exititem.setmnemonic(keyevent.vk_q);
        filemenu.add(exititem);
        exititem.addactionlistener(event -> system.exit(0));
    }
}

 

运行后打开图片:

图片查看器