[Java] [Component and event handling] Picture viewing software

Picture viewing software, supports 6 pictures. By clicking different buttons, you can view different pictures.

Insert picture description here
MyFrame:

package com.itheima;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame{
    
    
    JButton button1,button2,button3
            ,button4,button5,button6;
    JLabel label;
    PictureListener pictureListener;

    public MyFrame() {
    
    
        init();
        setVisible(true);
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    void init(){
    
    
        setLayout(new FlowLayout());
        button1 = new JButton("图片1");
        button1.setFont(new Font("微软雅黑",Font.PLAIN,12));//设置按钮字体和大小
        add(button1);
        button2 = new JButton("图片2");
        button2.setFont(new Font("微软雅黑",Font.PLAIN,12));
        add(button2);
        button3 = new JButton("图片3");
        button3.setFont(new Font("微软雅黑",Font.PLAIN,12));
        add(button3);
        button4 = new JButton("图片4");
        button4.setFont(new Font("微软雅黑",Font.PLAIN,12));
        add(button4);
        button5 = new JButton("图片5");
        button5.setFont(new Font("微软雅黑",Font.PLAIN,12));
        add(button5);
        button6 = new JButton("图片6");
        button6.setFont(new Font("微软雅黑",Font.PLAIN,12));
        add(button6);
        label = new JLabel();
        label.setPreferredSize(new Dimension(180,150));//设置标签大小
        add(label);
        pictureListener = new PictureListener();
        button1.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                ImageIcon icon = new ImageIcon("1.jpg");
                Image temp = icon.getImage().getScaledInstance(label.getWidth(),//将图片大小缩放成与标签同比例
                        label.getHeight(), icon.getImage().SCALE_DEFAULT);
                icon = new ImageIcon(temp);
                label.setIcon(icon);
            }
        });
        button2.addActionListener(pictureListener);
        button3.addActionListener(pictureListener);
        button4.addActionListener(pictureListener);
        button5.addActionListener(pictureListener);
        button6.addActionListener(pictureListener);


    }
    private class PictureListener implements ActionListener {
    
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
    
            if(e.getSource() == button2){
    
    
                ImageIcon icon = new ImageIcon("2.jpg");
                Image temp = icon.getImage().getScaledInstance(label.getWidth(),
                        label.getHeight(), icon.getImage().SCALE_DEFAULT);
                icon = new ImageIcon(temp);
                label.setIcon(icon);
            }
            if(e.getSource() == button3){
    
    
                ImageIcon icon = new ImageIcon("3.jpg");
                Image temp = icon.getImage().getScaledInstance(label.getWidth(),
                        label.getHeight(), icon.getImage().SCALE_DEFAULT);
                icon = new ImageIcon(temp);
                label.setIcon(icon);
            }
            if(e.getSource() == button4){
    
    
                ImageIcon icon = new ImageIcon("4.jpg");
                Image temp = icon.getImage().getScaledInstance(label.getWidth(),
                        label.getHeight(), icon.getImage().SCALE_DEFAULT);
                icon = new ImageIcon(temp);
                label.setIcon(icon);
            }
            if(e.getSource() == button5){
    
    
                ImageIcon icon = new ImageIcon("5.jpg");
                Image temp = icon.getImage().getScaledInstance(label.getWidth(),
                        label.getHeight(), icon.getImage().SCALE_DEFAULT);
                icon = new ImageIcon(temp);
                label.setIcon(icon);
            }
            if(e.getSource() == button6){
    
    
                ImageIcon icon = new ImageIcon("6.jpg");
                Image temp = icon.getImage().getScaledInstance(label.getWidth(),
                        label.getHeight(), icon.getImage().SCALE_DEFAULT);
                icon = new ImageIcon(temp);
                label.setIcon(icon);
            }
        }
    }
}

The listener for button1 is implemented with an anonymous inner class.

Main:

package com.itheima;
public class Main {
    
    
    public static void main(String[] args) {
    
    
        MyFrame myFrame = new MyFrame();
        myFrame.setBounds(100,100,240,300);
        myFrame.setTitle("匿名内部类的简单应用");
    }
}

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Note: The image path is placed under the project path.

Guess you like

Origin blog.csdn.net/weixin_48180029/article/details/112093288