本 Wiki 开启了 HTTPS。但由于同 IP 的 Blog 也开启了 HTTPS,因此本站必须要支持 SNI 的浏览器才能浏览。为了兼容一部分浏览器,本站保留了 HTTP 作为兼容。如果您的浏览器支持 SNI,请尽量通过 HTTPS 访问本站,谢谢!
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
cs:programming:java:courses:gtx_cs1311x:exceptions_ds_rec_gui [2024/01/30 13:12] – [入口函数的定义] codinghare | cs:programming:java:courses:gtx_cs1311x:exceptions_ds_rec_gui [2024/01/31 06:05] (当前版本) – [Addtional Operations] codinghare | ||
---|---|---|---|
行 690: | 行 690: | ||
} | } | ||
</ | </ | ||
+ | ==结构总览== | ||
+ | * // | ||
+ | * // | ||
+ | ==Events Handling== | ||
+ | //Events// 代表了用户的操作。JavaFX 中的 //Event// 继承自是 // | ||
+ | \\ \\ | ||
+ | {{ : | ||
+ | \\ \\ | ||
+ | 如果需要处理 // | ||
+ | <code js> | ||
+ | btn.setOnAction(new CustomEventHandler()); | ||
+ | </ | ||
+ | //Events// 的初始化需要 '' | ||
+ | * //Handler// 中必须定义一个 '' | ||
+ | * 通过继承接口 '' | ||
+ | * 用户需要根据 //Event// 的类型决定 '' | ||
+ | 比如为点击按钮设置一个名为 '' | ||
+ | <code js> | ||
+ | // | ||
+ | //click button action has type ActionEvent | ||
+ | private class CustomEventHandler implements EventHandler< | ||
+ | |||
+ | //rewrite handle | ||
+ | //handle parameter has type T to ensure the parameter macting the event type | ||
+ | public void handle(ActionEvent event) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | 当 '' | ||
+ | * '' | ||
+ | ==Panes & Scene Graphs== | ||
+ | Panes 为整个 //Stage// 提供布局。其扮演了一个组件容器的角色: | ||
+ | <code js> | ||
+ | StackPane root = new StackPane(); | ||
+ | </ | ||
+ | <WRAP center round info 100%> | ||
+ | 之后创建的组件会叠加到之前创建的组件**上面**。 | ||
+ | </ | ||
+ | 实例化 Pane 之后,我们就可以将之间创建的组件通过 '' | ||
+ | <code js> | ||
+ | //adding button element to the container | ||
+ | root.getChildren().add(btn); | ||
+ | </ | ||
+ | Pane 与组件之间的关系是**包含(// | ||
+ | \\ \\ | ||
+ | {{ : | ||
+ | \\ | ||
+ | 其他可选的 layout 和组件有: | ||
+ | \\ \\ | ||
+ | {{ : | ||
+ | \\ \\ | ||
+ | * //Layout// 类别中的类都是 '' | ||
+ | * 元素组件被称为 '' | ||
+ | * //Layout// 本身不必是根部节点,也可以是**分支**节点(嵌套布局) | ||
+ | <WRAP center round tip 100%> | ||
+ | [[https:// | ||
+ | </ | ||
+ | |||
+ | ===Anonymous Inner Classes=== | ||
+ | EventHandler 可以用 inner class 的方式实现: | ||
+ | <code js> | ||
+ | EventHandler< | ||
+ | public void handle(ActionEvent event) { | ||
+ | System.out.println(" | ||
+ | }; | ||
+ | }; | ||
+ | //notice CustomEventHandler is not a cstr. it is an reference name now. | ||
+ | btn.setOnAction(CustomEventHandler); | ||
+ | < | ||
+ | 如果只使用一次,可以写成匿名的形式直接作为 '' | ||
+ | <code js> | ||
+ | btn.setOnAction( | ||
+ | new EventHandler< | ||
+ | public void handle(ActionEvent event) { | ||
+ | System.out.println(" | ||
+ | }; | ||
+ | } | ||
+ | ); | ||
+ | </ | ||
+ | ===Lambda Expressions=== | ||
+ | 上述的 EventHandler 还可以使用 Lambda 表达式进一步的改进: | ||
+ | <code js> | ||
+ | btn.setOnAction(event -> System.out.println(" | ||
+ | </ | ||
+ | * '' | ||
+ | * '' | ||
+ | * 如果 '' | ||
+ | ===Addtional Operations=== | ||
+ | <WRAP center round download 100%> | ||
+ | {{ : | ||
+ | </ | ||
+ | |||
+ | ==ComboBox== | ||
+ | * 定义 ComboBox | ||
+ | <code js> | ||
+ | ComboBox< | ||
+ | </ | ||
+ | * 添加选项到 Combobox | ||
+ | <code js> | ||
+ | pickScaleFrom.getItems().addAll(" | ||
+ | </ | ||
+ | * 设定默认选项 | ||
+ | <code js> | ||
+ | pickScaleFrom.getSelectionModel().selectFirst(); | ||
+ | pickScaleTo.getSelectionModel().selectLast(); | ||
+ | </ | ||
+ | * 从 ComboBox 中获取选项值 | ||
+ | <code js> | ||
+ | | ||
+ | </ | ||
+ | * 打印值到 Label 区域 | ||
+ | <code js> | ||
+ | | ||
+ | </ | ||
+ | ==Label== | ||
+ | * 创建 Label | ||
+ | <code js> | ||
+ | abel from = new Label(" | ||
+ | </ | ||
+ | ==TextField== | ||
+ | * 创建 TextField | ||
+ | <code js> | ||
+ | TextField userInput = new TextField(); | ||
+ | </ | ||
+ | * 获取 TextField 中的内容(in string format) | ||
+ | <code js> | ||
+ | //userInput is an TextField instance | ||
+ | userInput.getCharacters().toString(); | ||
+ | </ | ||
+ | ==Error dialog box== | ||
+ | {{ : | ||
+ | <code js> | ||
+ | //define | ||
+ | Alert a = new Alert(AlertType.ERROR); | ||
+ | //title | ||
+ | a.setTitle(" | ||
+ | //error message | ||
+ | a.setHeaderText(" | ||
+ | //details | ||
+ | a.setContentText(" | ||
+ | //keeping the dialog box show up, pause the program | ||
+ | a.showAndWait(); | ||
+ | </ | ||
+ | ==Layout== | ||
+ | * 创建 Layout | ||
+ | <code js> | ||
+ | //keep the element in the same row | ||
+ | HBox input = new HBox(); | ||
+ | // | ||
+ | VBox scales = new VBox(); | ||
+ | </ | ||
+ | * 设置对齐方式: | ||
+ | <code js> | ||
+ | input.setAlignment(Pos.CENTER); | ||
+ | </ | ||
+ | * 设置间距 | ||
+ | <code js> | ||
+ | scales.setSpacing(10); | ||
+ | </ | ||
+ | * Layout 的嵌套 | ||
+ | <code js> | ||
+ | root.getChildren().addAll(input, | ||
+ | </ | ||
+ |