JavaFX 笔记

起始模板

  • Application
//MainWindow.java
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainWindow extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("main.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 500, 420));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
  • controller
//MainController.java
public class MainController implements Initializable {
    @FXML
    private Label label1;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }
}
  • fxml 文件中完成 fxml对controller的绑定,放在布局的根元素的属性下,debug的时候注意检查
fx:controller="MainController"

数据交互

  • Application 中调用Controller中的方法
public class MainWindow extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception { 
        //必须采用动态创建root的方式
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
        Parent root = fxmlLoader.load();
        //获取Controller对象
        MainController controller = fxmlLoader.getController();
        //调用controller的方法        
        controller.function();
    }
}
  • Controller 中获取Application的Stage对象
public class MainController implements Initializable {
    @FXML
    private FlowPane rootLayout; //此处的类型为fxml布局的根元素的类型

    private Stage getStage() {
        //获取stage的方式
        Stage stage = (Stage) rootLayout.getScene().getWindow();
        return stage;
    }
}

理想的自定义子窗体的创建方法

为新窗体同样创建Application类,Controller类和fxml文件,保证文件原有结构,在子窗体的构造函数中调用创建窗体的方法,并且可以设置窗体是否为模态。之后在要创建窗体的位置new一个新窗体的对象即可。

public class SubWindow extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("sub.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 500, 420));
        primaryStage.show();
    }
    public SubWindow(){
        Stage stage = new Stage();
        stage.initModality(Modality.APPLICATION_MODAL);//模态窗体
        //非模态对应 Modality.NONE
        this.start(stage);
    }
}
new SubWindow(); //调用构造函数创建子窗体

内置对话框

  • Alert
Alert alert = new Alert(Alert.AlertType.NONE, "hello", ButtonType.OK, ButtonType.CANCEL);
alert.setTitle("Hello");
Optional r = alert.showAndWait(); 
if (r.get()==ButtonType.OK){ //获取返回值的方式
    //...
} 
  • FileChooser
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("open pics");
fileChooser.getExtensionFilters().addAll(
    new FileChooser.ExtensionFilter("img", "*.jpg;*.png"),
    new FileChooser.ExtensionFilter("gif", "*.gif")
);
Stage stage = (Stage) rootLayout.getScene().getWindow();
File file = fileChooser.showOpenDialog(stage);
  • 待补充
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容