图片查看器

2019-08-26 05:57:46来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

图片查看器

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

 

运行后打开图片:


原文链接:https://www.cnblogs.com/sunzhongyu008/p/11372392.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Dubbo

下一篇:mini QQ(项目一)