Preloader for JavaFX learning

6072 단어 JavaFXpreloader
JavaFX provides the Proloader application when the Application is loaded, which can display the loading process bar or others notification.

Main Application

package main;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import preloader.TestPreloader;

/**
 *
 * @author lq
 */
public class MainApplication extends Application {
	
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Preloader

package preloader;

import javafx.application.Preloader;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * Simple Preloader Using the ProgressBar Control
 *
 * @author lq
 */
public class TestPreloader extends Preloader {
    
    ProgressBar bar;
    Stage stage;
    
    private Scene createPreloaderScene() {
        bar = new ProgressBar();
        BorderPane p = new BorderPane();
        p.setCenter(bar);
        return new Scene(p, 300, 150);        
    }
    @Override
    public void init(){
			System.out.println("preloader init");
    	
    }
    
    @Override
    public void start(Stage stage) throws Exception {
    	System.out.println("preloader start");
        this.stage = stage;
        stage.setScene(createPreloaderScene());        
        stage.show();
    }
    
    @Override
    public void handleStateChangeNotification(StateChangeNotification scn) {
        if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
            stage.hide();
        }
    }
    
    @Override
    public void handleProgressNotification(ProgressNotification pn) {
        bar.setProgress(pn.getProgress());
    }    
    
    public static void main(String args[]){
    	launch();
    }
}
If you use NetBeans, you can choose the Preloader program when you build the Main Application, and you only need to run the Main program when you run it.

If you use eclipse, you need to use Ant

<project name="TestPreloader" default="default" basedir="." xmlns:fx="javafx:com.sun.javafx.tools.ant">
	<target name="default">
		<taskdef resource="com/sun/javafx/tools/ant/antlib.xml" uri="javafx:com.sun.javafx.tools.ant" classpath=".:C:/Program Files/Java/jdk1.7.0_07/lib/ant-javafx.jar" />
		<fx:application id="app-desc" mainClass="main.MainApplication" preloaderClass="preloader.TestPreloader">
		</fx:application>
		<fx:jar destfile="dist/application.jar">
		    <fx:application refid="app-desc"/>
			<fileset dir="bin"/>
		</fx:jar>
        <java jar="dist/application.jar" fork='true'/>
	</target>
</project>
Mainly fx:application, it will help you link main and preloader, and then generate jar package. Then you can execute the jar package (in the end, it becomes the execution jar, which is what you think, but it always feels a bit different from netBeans).

Preloader has 3 methods to overload
public void handleStateChangeNotification(StateChangeNotification scn) is called three times in total, before main starts load, init, start.
public void handleProgressNotification(ProgressNotification pn) Secondary 0 and 1, all before main load.
public void handleApplicationNotification(PreloaderNotification info) Called only when notifyPreloader is called in the main function.

With 2 stages, there are 2 situations for communication between them:
One: Get the main Application through handleStateChangeNotification(StateChangeNotification evt) evt.getApplication. Generally, the preloader provides an interface, and the main implements the interface, so that the preloader can obtain the content of the main through the interface.
Two: PreloaderHandoverEvent implements preloaderNotification, then main saves the node to the PreloaderHandoverEvent class, then calls the notifyPreloader method, and the preloader obtains the final PreloaderHandoverEvent event = (PreloaderHandoverEvent) info through handleApplicationNotification(PreloaderNotification info); the implementation class, and then obtains the node.

There are a lot of javafx games here, written by a master

http://lust heat Zhang.goto IP4.com/FX game/

There is a Chinese website for javafx learning, and I threw all the content there.
http://www.jfxee.com/

좋은 웹페이지 즐겨찾기