Java的小游戏加载界面
时间: 2023-08-08 13:12:40 浏览: 92
Java的小游戏加载界面可以使用Swing或JavaFX来实现。以下是一个使用Swing的加载界面示例:
```java
import javax.swing.*;
import java.awt.*;
public class GameLoadingScreen extends JFrame {
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
private static final int BAR_WIDTH = 300;
private JProgressBar progressBar;
public GameLoadingScreen() {
super("Game Loading Screen");
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("Loading...");
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label, BorderLayout.NORTH);
progressBar = new JProgressBar(0, 100);
progressBar.setPreferredSize(new Dimension(BAR_WIDTH, 30));
progressBar.setStringPainted(true);
panel.add(progressBar, BorderLayout.CENTER);
add(panel);
setVisible(true);
}
public void updateProgressBar(int value) {
progressBar.setValue(value);
}
public void close() {
setVisible(false);
dispose();
}
}
```
使用该类的示例代码如下:
```java
public class Game {
private GameLoadingScreen loadingScreen;
public void start() {
loadingScreen = new GameLoadingScreen();
loadingScreen.setVisible(true);
// 模拟加载过程
for (int i = 0; i <= 100; i += 10) {
loadingScreen.updateProgressBar(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
loadingScreen.close();
// 游戏开始
}
}
```
在游戏加载时创建GameLoadingScreen实例并显示,通过updateProgressBar方法更新进度条的值,加载完成后调用close方法关闭加载界面。
阅读全文