JAVA项目之天天酷跑-Day03


项目借鉴: https://blog.csdn.net/qq_45909299

加载界面制作

需求分析

  • 加载界面在新窗口中展示
  • 添加进度条
  • 加载界面背景图片
  • 采用多线程模式实现加载界面

WindowFrame类制作

继承JFrame并实现多线程

public class WindowFrame extends JFrame implements Runnable {
	@Override
	public void run() {
	
	}
}

添加背景和进度条

添加全局变量background,jdt

JLabel background;
JProgressBar jdt;//进度条

添加构造方法

public WindowFrame() {

}
设置背景
String path;
path = getClass().getClassLoader().getResource("resources/images/hbg.jpg").getPath();
background = new JLabel(new ImageIcon(path));
this.add(BorderLayout.NORTH, background);//放在窗口上
进度条实现
jdt = new JProgressBar();
jdt.setStringPainted(true);//加载以字符串形式显示
jdt.setBackground(Color.ORANGE);
this.add(BorderLayout.SOUTH, jdt);
设置窗口的基本属性
this.setSize(568, 340);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setUndecorated(true);
path = getClass().getClassLoader().getResource("resources/images/115.png").getPath();
this.setIconImage(new ImageIcon(path).getImage());
this.setVisible(true);
this.setAlwaysOnTop(true);//设置窗口始终置顶

实现多线程的run方法

@Override
public void run() {
    int[] values = {0, 1, 3, 10, 23, 32, 40, 47, 55, 66, 76, 86, 89, 95, 99, 99, 99, 100};
    for (int value : values) {
        jdt.setValue(value);
        try {
            Thread.sleep(200);//200ms
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在MainFrame中添加代码

@Override
public void mouseClicked(MouseEvent e) {
    if(e.getSource().equals(start)) {
        //跳转到下一界面
        new WindowFrame().Start();
        //new WindowFrame().start();
    }else if(e.getSource().equals(exit)) {
        dispose();
    }else if(e.getSource().equals(help)) {
        JOptionPane.showMessageDialog(null, "有问题请联系开发者");
    }
}

最后附上代码

package cn.sqc.runday.controller;

import javax.swing.*;
import java.awt.*;

/**
 * @author THEO
 * @date 2021-3-25
 */

public class WindowFrame extends JFrame implements Runnable {

    JLabel background;
    JProgressBar jdt;//进度条

    //创建线程并启动
    public void Start() {
        WindowFrame frame = new WindowFrame();
        Thread t = new Thread(frame);
        t.start();
        dispose();
    }

    public WindowFrame() {
        String path;
        path = getClass().getClassLoader().getResource("resources/images/hbg.jpg").getPath();
        background = new JLabel(new ImageIcon(path));
        this.add(BorderLayout.NORTH, background);//放在窗口上

        jdt = new JProgressBar();
        jdt.setStringPainted(true);//加载以字符串形式显示
        jdt.setBackground(Color.ORANGE);
        this.add(BorderLayout.SOUTH, jdt);

        this.setSize(568, 340);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setUndecorated(true);
        path = getClass().getClassLoader().getResource("resources/images/115.png").getPath();
        this.setIconImage(new ImageIcon(path).getImage());
        this.setVisible(true);
        this.setAlwaysOnTop(true);
    }

    @Override
    public void run() {
        int[] values = {0, 1, 3, 10, 23, 32, 40, 47, 55, 66, 76, 86, 89, 95, 99, 99, 99, 100};
        for (int value : values) {
            jdt.setValue(value);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new WindowFrame().Start();
    }
}

Finished.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值