1、到官网下载对应的Java FX SDK开发包:https://gluonhq.com/products/javafx/
2、打开运行/调试设置 ====>配置虚拟机选项:
先引入lib包,指定lib文件夹
紧接着配置虚拟机选项:
--module-path “JavaFx SDK的lib文件夹路径C:\javafx-sdk-15.0.1\lib” --add-modules javafx.controls,javafx.fxml
这一步很重要,记得要选一下对应的版本模块
3、运行一下简单的示例项目
4、主要代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javax.annotation.processing.Messager;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); //加载布局文件
primaryStage.setTitle("JavaFx入门"); //设置标题
primaryStage.setScene(new Scene(root, 100, 100)); //初始化容器场景
Button btn=new Button("点我"); //新建一个Button
btn.setOnAction(this::handler); //点击触发事件
BorderPane bp=new BorderPane(); //设置容器
bp.setCenter(btn);//将控件添加进容器
Scene sc=new Scene(bp, 500, 500); //设置场景,将容器添加进场景中并设置宽高
primaryStage.setScene(sc); //设置场景应用到初始化容器场景中
primaryStage.show(); //显示场景页面
}
//点击事件
public void handler(ActionEvent event) {
f_alert_informationDialog("提示信息","你好呀。",null,null );
}
// 弹出一个信息对话框
public void f_alert_informationDialog(String p_title,String p_header, String p_message, Window d_stage){
Alert _alert = new Alert(Alert.AlertType.INFORMATION);
_alert.setTitle(p_title);
_alert.setHeaderText(p_header);
_alert.setContentText(p_message);
_alert.initOwner(d_stage);
_alert.show();
}
//程序入口
public static void main(String[] args) {
launch(args);
}
}