JavaSwing开发的电子相册,可以播放音乐,切换照片
import javazoom.jl.player.Player;import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;/*** @author 专治八阿哥的孟老师* 谨以此程序,献给所有奋斗的人们*/
public class TeachAlbum {public static void main(String[] args) {playMusic("C:\\Users\\Music\\风雨人生.mp3");JFrame jfm = new JFrame();// 创建窗体大小jfm.setSize(1250, 753);// 创建面板并添加到面板上MyPanel panel = new MyPanel();jfm.add(panel);jfm.setLocationRelativeTo(null);// 设置关闭窗体时关闭程序jfm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体可见jfm.setVisible(true);}public static void playMusic(String path) {//播放背景音乐new Thread(new Runnable() {@Overridepublic void run() {File file = new File(path);try {FileInputStream fis = new FileInputStream(file);BufferedInputStream stream = new BufferedInputStream(fis);Player player = new Player(stream);player.play();} catch (Exception e) {e.printStackTrace();}}}).start();}
}class MyPanel extends JPanel {JLabel pictureLabel = null;public MyPanel() {super.setLayout(null);//清除默认样式//因为先添加的层级在最前面Image logo = new ImageIcon(this.getClass().getResource("logo.png")).getImage().getScaledInstance(364, 360, Image.SCALE_DEFAULT);pictureLabel = new JLabel(new ImageIcon(logo));pictureLabel.setBounds(586, 84, 364, 364);// 设置位置this.add(pictureLabel);//相框Image border = new ImageIcon(this.getClass().getResource("border.jpg")).getImage().getScaledInstance(641, 464, Image.SCALE_DEFAULT);JLabel imgLabel = new JLabel(new ImageIcon(border));imgLabel.setBounds(440, 33, 641, 464);// 设置位置this.add(imgLabel);playPictures();//播放照片}@Overridepublic void paintComponent(Graphics g) {try {//初始化背景BufferedImage bg = ImageIO.read(this.getClass().getResource("bg.jpg"));Graphics2D g2 = (Graphics2D) g;g2.drawImage(bg.getScaledInstance(1250, 753, Image.SCALE_DEFAULT), 0, 0,1250, 753, null);} catch (Exception e) {e.printStackTrace();}}void playPictures() {//播放相片new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(5000);pictureLabel.setBounds(454, 82, 614, 366);// 设置位置URL url = MyPanel.class.getResource("imgs");File dir = new File(url.getFile());File[] pics = dir.listFiles();for (File pic : pics) {Image image = new ImageIcon(pic.getAbsolutePath()).getImage().getScaledInstance(612, 366,Image.SCALE_DEFAULT);pictureLabel.setIcon(new ImageIcon(image));Thread.sleep(5000);}} catch (InterruptedException e) {e.printStackTrace();}}}).start();}
}
Swing is a development toolkit for developing user interfaces for Java applications.
Based on the Abstract Window Toolkit (AWT), cross platform applications can use any pluggable appearance style. Swing developers can leverage Swing's rich and flexible functionality and modular components to create elegant user interfaces with minimal code. All packages in the toolkit are named with swing, such as javax. swing, javax. swing. event.
(1) Import Swing Package
(2) Choose an interface style
(3) Set top-level container
(4) Set buttons and labels
(5) Place the component on the container
(6) Add borders to components
(7) Handling Events
(8) Auxiliary technical support