【Java】【组件及其事件处理】图片查看软件
作者:互联网
图片查看软件,支持6张图片。通过点击不同的按钮,可以查看不同的图片。
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);
}
}
}
}
button1的监听器是用匿名内部类实现的。
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("匿名内部类的简单应用");
}
}
注意: 图片路径是放在项目路径下的。
标签:事件处理,getImage,Java,ImageIcon,label,组件,new,Font,icon 来源: https://blog.csdn.net/weixin_48180029/article/details/112093288