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.