JavaFX程序主要有两种启动方式:
- 在实现了
java.application.Application
的实现类中使用launch()
方法启动
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class StartingModeOneApplication extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("第一种启动方式");
StackPane stackPane = new StackPane();
stackPane.getChildren().add(label);
Scene scene = new Scene(stackPane, 600, 480);
stage.setScene(scene);
stage.show();
}
}
- 通过
Application.launch()
方法启动实现了java.application.Application
的类(可以将逻辑和启动从代码层面分开)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SecondApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("第二种启动方式");
StackPane stackPane = new StackPane();
stackPane.getChildren().add(label);
Scene scene = new Scene(stackPane, 600, 480);
stage.setScene(scene);
stage.show();
}
}
import javafx.application.Application;
public class StartingModeSecondApplication {
public static void main(String[] args) {
// 这种方式可以启动继承了javafx.application.Application的类
Application.launch(SecondApplication.class, args);
}
}