JavaFx UI控件与代码间的绑定方法
作者:大大大超人 时间:2021-09-13 00:40:27
JavaFx初探一,UI控件的使用,具体内容如下
方式一:使用纯代码直接new view控件,这样就不涉及到与fxml文件之间的交互了
方式二:使用fxml编写界面文件,用可视化工具scene builder 来构建交互界面。
分两种方式绑定控件,可以用Android的方法在java中直接调用类似findviewbyId的方法,先在xml文件中设置fxid,然后根据id去找这个控件,代码如下
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
BorderPane borderPane = (BorderPane)root.lookup("#iiid");
TableView tableView = new TableView();
TableColumn columnName = new TableColumn("姓名");
TableColumn columClass = new TableColumn("班级");
TableColumn columNum = new TableColumn("学号");
tableView.getColumns().addAll(columnName, columClass, columNum);
borderPane.setCenter(tableView);
primaryStage.setTitle("应用测试");
primaryStage.setScene(new Scene(root,500,500));
primaryStage.show();
}
方式二
同样需要设置id,不过可以通过给每个控件设置一个控制器类的方式,去获取控件实例,在fxml文件中增加控制类的声明 fx:controller=”sample.Controller”
注意:只能在根部局容器中才能设置这个控制器,设置好后,鼠标选中id 快捷键alt+enter 即可在对应的控制类中生成控件的对象(好像是根据注解映射来的)
控制器类也可以设置为main类,不用单独新建一个类,根据情况定
<BorderPane fx:id="iiid" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<Button fx:id="button1" mnemonicParsing="false" text="按钮1" BorderPane.alignment="CENTER"/>
</top>
</BorderPane>
鼠标点击事件可以在 on action中定义一个方法, 然后控制类中去实现这个方法,在该控件被点击时就会被触发。
来源:https://blog.csdn.net/superman4933/article/details/81410858
标签:JavaFx,UI,代码间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java SpringBoot实现带界面的代码生成器详解
2023-09-28 11:54:14
![](https://img.aspxhome.com/file/2023/0/63000_0s.png)
SpringBoot自定义注解实现Token校验的方法
2023-11-13 23:17:52
Springboot内置的工具类之CollectionUtils示例讲解
2023-08-23 20:47:13
![](https://img.aspxhome.com/file/2023/0/58440_0s.jpg)
Kotlin与Java相互调用的完整实例
2023-06-17 03:23:23
Android SharedPreferences存储用法详解
2023-08-07 08:25:34
![](https://img.aspxhome.com/file/2023/8/83528_0s.gif)
Spring(AbstractRoutingDataSource)实现动态数据源切换示例
2022-05-23 07:05:08
![](https://img.aspxhome.com/file/2023/1/64111_0s.jpg)
SpringBoot整合MybatisPlus实现增删改查功能
2022-10-31 05:24:15
![](https://img.aspxhome.com/file/2023/8/65728_0s.png)
java 非对称加密算法RSA实现详解
2023-11-25 07:41:36
Flutter 容器盒子模型的使用示例
2023-06-18 18:47:43
![](https://img.aspxhome.com/file/2023/4/85974_0s.png)
浅析SpringBoot2底层注解@Conditional@ImportResource
2023-08-01 23:35:51
![](https://img.aspxhome.com/file/2023/3/61793_0s.png)
Java ThreadLocal的使用详解
2023-11-29 04:48:43
流读取导致StringBuilder.toString()乱码的问题及解决
2022-12-20 13:34:14
Java使用二分法进行查找和排序的示例
2023-03-16 10:16:37
图解JVM垃圾内存回收算法
2023-10-13 17:24:35
![](https://img.aspxhome.com/file/2023/4/64804_0s.jpg)
Java 多线程同步 锁机制与synchronized深入解析
2023-12-20 17:55:50
浅谈Java内存区域划分和内存分配策略
2023-08-11 18:52:49
![](https://img.aspxhome.com/file/2023/6/58146_0s.png)
Java SpringBoot自动装配原理详解
2022-09-08 01:15:09
![](https://img.aspxhome.com/file/2023/0/61610_0s.png)
JAVA8 Stream流中的reduce()方法详解
2023-09-01 21:10:33
Unity2D实现游戏回旋镖
2022-07-16 07:13:50
![](https://img.aspxhome.com/file/2023/8/66118_0s.gif)
IOS 实现摇一摇的操作
2023-07-02 13:46:53