JavaFx 启动界面制作(类似IDEA启动界面)

目的

一个简单的淡入显示界面,进度条加载完毕后关闭,打开程序的主界面。

界面展示

JavaFx 启动界面制作(类似IDEA启动界面)_第1张图片

css代码

.progress-bar .bar {  
    -fx-padding: 2px;  
    -fx-background-insets: 0;  
    -fx-background-color: #60ee92;  
}

java代码

package com.danbobo;  
  
/**  
 * @ClassName: JavaFxPopup  
 * @Description:  
 * @author: Danbobo  
 * @date: 2023/4/15 13:59  
 */import javafx.animation.FadeTransition;  
import javafx.animation.KeyFrame;  
import javafx.animation.Timeline;  
import javafx.application.Application;  
import javafx.concurrent.Task;  
import javafx.geometry.Pos;  
import javafx.scene.Group;  
import javafx.scene.Scene;  
import javafx.scene.control.Label;  
import javafx.scene.control.ProgressBar;  
import javafx.scene.layout.AnchorPane;  
import javafx.scene.layout.Background;  
import javafx.scene.layout.Pane;  
import javafx.scene.paint.Color;  
import javafx.scene.shape.Rectangle;  
import javafx.stage.Stage;  
import javafx.stage.StageStyle;  
import javafx.util.Duration;  
  
public class JavaFxPopup extends Application {  
  
    private static final int WIDTH = 600;  
    private static final int HEIGHT = 350;  
  
    // 创建一个耗时任务  
    Task<Void> task = new Task<Void>() {  
        @Override  
        protected Void call() throws Exception {  
            for (int i = 0; i < 100; i++) {  
                updateProgress(i + 1, 100); // 更新进度条的值  
                Thread.sleep(25); // 模拟一个耗时操作  
            }  
            return null;  
        }  
    };  
  
    @Override  
    public void start(Stage primaryStage) {  
  
        primaryStage.initStyle(StageStyle.TRANSPARENT);  
  
        //根节点  
        AnchorPane root = new AnchorPane();  
        root.setPrefSize(WIDTH, HEIGHT);  
        root.setStyle("-fx-background-image: url('img/cloud.jpeg');-fx-background-size: contain");  
  
        FadeTransition fadeOutTransition = new FadeTransition(Duration.millis(2500), root);  
        fadeOutTransition.setFromValue(0.0); // 起始透明度  
        fadeOutTransition.setToValue(1.0); // 目标透明度  
        fadeOutTransition.setOnFinished(e -> {  
            new Thread(task).start();  
        });  
        fadeOutTransition.play();  
  
        //界面文字  
        Label label = new Label("云与月 Judge");  
        label.setTextFill(Color.WHITE);  
        label.setStyle("-fx-font-size: 24;-fx-font-family: Inconsolata;-fx-font-weight: bold");  
        root.getChildren().add(label);  
        AnchorPane.setTopAnchor(label,150.0);  
        AnchorPane.setLeftAnchor(label,230.0);  
  
  
        //加载进度条  
        ProgressBar progressBar = new ProgressBar(0.0);  
        //设置样式  
        progressBar.getStyleClass().add("progress-bar");  
        progressBar.setOpacity(0.7);  
        // 绑定进度条的值与任务进度  
        progressBar.progressProperty().bind(task.progressProperty());  
        progressBar.setPrefWidth(WIDTH);  
        root.getChildren().add(progressBar);  
        AnchorPane.setBottomAnchor(progressBar,0.0);  
        progressBar.progressProperty().addListener((observable, oldValue, newValue)->{  
			//进度条100%时操作
            if (newValue.doubleValue()==1.0){  
                primaryStage.close();  
            }  
        });  
  
        Scene scene = new Scene(new Group(root), WIDTH, HEIGHT, Color.TRANSPARENT);  
        //加载css样式表  
        scene.getStylesheets().add("css/my-progress-bar.css");  
        primaryStage.setScene(scene);  
        primaryStage.setMinWidth(WIDTH);  
        primaryStage.setMinHeight(HEIGHT);  
        primaryStage.show();  
    }  
  
    public static void main(String[] args) {  
        launch(args);  
    }  
}

你可能感兴趣的:(intellij-idea,java,JavaFx)