天天看点

《JAVA筑基100例》「第39题」JAVA高级技术-内部类3(匿名内部类

作者:小虚竹分享技术

#头条创作挑战赛#

❤️作者简介:大家好,我是小虚竹。Java领域优质创作者,CSDN博客专家,华为云享专家,掘金年度人气作者,阿里云专家博主,51CTO专家博主

❤️技术活,该赏

❤️点赞 收藏 ⭐再看,养成习惯

零、前言

今天是学习 「JAVA语言」 打卡的第39天,我的学习策略很简单,题海策略+ 费曼学习法。如果能把这100题都认认真真自己实现一遍,那意味着 「JAVA语言」 已经筑基成功了。后面的进阶学习,可以继续跟着我,一起走向架构师之路。

一、题目描述

题目:在类中,除以可以定义参数,方法和块,还可以定义类。这种类叫做内部类。

实现:写一个简单的图片查看软件,支持6张图片,通过单击查看不同的图片。

二、解题思路

写一个图片查看类ImageViewer,这个类继承了JFrame。

在窗体里定义6个按钮和一个标签

「当只需要创建类的一个对象时,可以使用匿名内部类」

三、代码详解

图片查看类:

public class ImageViewer extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1311689308422117198L;
    private JPanel contentPane;
    private JLabel label;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImageViewer frame = new ImageViewer();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ImageViewer() {
        setTitle("JAVA高级技术-内部类3(匿名内部类)");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 300, 240);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(new BorderLayout(0, 0));

        JPanel buttonPanel = new JPanel();
        panel.add(buttonPanel, BorderLayout.NORTH);

        JButton button1 = new JButton("图片1");
        button1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = ImageViewer.class.getClassLoader().getResource("images/1.jpg").getPath();
                label.setIcon(new ImageIcon(fileName));
            }
        });
        buttonPanel.setLayout(new GridLayout(2, 3, 5, 5));
        buttonPanel.add(button1);

        JButton button2 = new JButton("图片2");
        button2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = ImageViewer.class.getClassLoader().getResource("images/2.jpg").getPath();
                label.setIcon(new ImageIcon(fileName));
            }
        });
        buttonPanel.add(button2);

        JButton button3 = new JButton("图片3");
        button3.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = ImageViewer.class.getClassLoader().getResource("images/3.jpg").getPath();
                label.setIcon(new ImageIcon(fileName));
            }
        });
        buttonPanel.add(button3);

        JButton button4 = new JButton("图片4");
        button4.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = ImageViewer.class.getClassLoader().getResource("images/4.jpg").getPath();
                label.setIcon(new ImageIcon(fileName));
            }
        });
        buttonPanel.add(button4);

        JButton button5 = new JButton("图片5");
        button5.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = ImageViewer.class.getClassLoader().getResource("images/5.jpg").getPath();
                label.setIcon(new ImageIcon(fileName));
            }
        });
        buttonPanel.add(button5);

        JButton button6 = new JButton("图片6");
        button6.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String fileName = ImageViewer.class.getClassLoader().getResource("images/6.jpg").getPath();
                label.setIcon(new ImageIcon(fileName));
            }
        });
        buttonPanel.add(button6);

        label = new JLabel("");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(label, BorderLayout.CENTER);
    }

}


           
《JAVA筑基100例》「第39题」JAVA高级技术-内部类3(匿名内部类
《JAVA筑基100例》「第39题」JAVA高级技术-内部类3(匿名内部类
《JAVA筑基100例》「第39题」JAVA高级技术-内部类3(匿名内部类

多学一个知识点

如何获取项目中的图片文件地址:

❝ ImageViewer.class.getClassLoader().getResource("images/6.jpg").getPath()

我是虚竹哥,我们下一题见~

继续阅读