美文网首页
JavaFX 笔记

JavaFX 笔记

作者: puzzledsky | 来源:发表于2018-10-26 09:31 被阅读0次

起始模板

  • 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);
  • 待补充

相关文章

  • JavaFX 笔记

    起始模板 Application controller fxml 文件中完成 fxml对controller的绑定...

  • JavaFx笔记

    定位:桌面端开发。 Stage:包含Title和Scene Scene 包含各种Pane(用FXML加载) Sce...

  • javafx笔记

    记录javafx使用 表格样式重写 其他css 事件监听 布局

  • JavaFX实现客户端——java也可以做很炫的页面哦

    (JavaFX实现客户端) JavaFX 技术栈 JavaFX+springboot+sqlite+mybatis...

  • JavaFx学习笔记

    最初,笔者在工作中需要为客户开发一个Excel批量数据处理工具,虽然实现的方式有很多(C#、Electron、Py...

  • JAVAFX事件 笔记

    1.JavaFX中的事件 1.1鼠标事件 1.2键事件 2.处理步骤 3.鼠标事件 在MouseEvent 对象里...

  • JavaFX相关参考

    1.JavaFX 简介 2.JavaFX应用 3. JAVAFX - Layout Pane 布局详解参考文章 4...

  • JavaFX - 应用

    原文地址:https://www.tutorialspoint.com/javafx/javafx_applica...

  • android studio--导入JavaFx

    JavaFx 网站https://gluonhq.com/products/javafx/[https://glu...

  • FX事件处理

    JavaFX中的事件 JavaFX提供了处理各种事件的支持。包javafx.event的名为Event的类是事件的...

网友评论

      本文标题:JavaFX 笔记

      本文链接:https://www.haomeiwen.com/subject/tjzhtqtx.html