【Java】【元件及其事件處理】圖片檢視軟體
阿新 • • 發佈:2021-01-10
技術標籤:# 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("匿名內部類的簡單應用");
}
}
注意: 圖片路徑是放在專案路徑下的。