Preloader for JavaFX learning
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/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Eclipse에서 javaFX 도입 (2020-4 월)javaSE에 javaFX가 동봉되지 않게 되어, 기존의 방법으로는 잘 되지 않는 것이 있는 것 같고, 도입에 망설였으므로 나중의 참고가 된다고 생각. 초보자이므로 손 부드럽게. macOS Catalina Eclip...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.