java制作电子相册

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() {
            @Override
            public 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();//播放照片
    }

    @Override
    public 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() {
            @Override
            public 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

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专治八阿哥的孟老师

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值