JavaFx在Main中获取Controller初始化相关配置和Fxml
Main中的方法
package sample; import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage; import java.net.URL;
import javafx.fxml.JavaFXBuilderFactory; public class Main extends Application { @Override
public void start(Stage primaryStage) throws Exception {
URL location = getClass().getResource("sample.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Parent root = fxmlLoader.load();
//如果使用 Parent root = FXMLLoader.load(...) 静态读取方法,无法获取到Controller的实例对象
primaryStage.setTitle("Hello World");
Scene scene = new Scene(root, 400, 500);
//加载css样式
//scene.getStylesheets().add(getClass().getResource("style1.css").toExternalForm());
primaryStage.setScene(scene);
Controller controller = fxmlLoader.getController(); //获取Controller的实例对象
//Controller中写的初始化方法
controller.Init();
primaryStage.show();
} public static void main(String[] args) {
launch(args);
}
}
Controller类:
package sample; import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ComboBox;
import javafx.event.ActionEvent;
import javafx.collections.ObservableList; public class Controller { @FXML
private Button mButton;
@FXML
private Label mLabel;
@FXML
private ComboBox comboBox_API; Integer ClickCount=0; private ObservableList<String> apiList=FXCollections.observableArrayList();
public void Init(){
apiList.add("创建订单");
apiList.add("获取订单信息");
this.comboBox_API.setItems(apiList);
this.mButton.setText("按钮1");
} @FXML
public void onButtonClick(ActionEvent event) {
ClickCount++;
mLabel.setText("HelloWorld"+ClickCount);
}
}
Fxml文件:
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.collections.ObservableList?> <GridPane alignment="center" hgap="10" prefHeight="400.0" prefWidth="500.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints prefWidth="60"/>
<ColumnConstraints prefWidth="60" />
<ColumnConstraints prefWidth="280"/>
</columnConstraints>
<rowConstraints>
<RowConstraints prefHeight="40"/>
<RowConstraints prefHeight="200" />
<RowConstraints prefHeight="40" />
<RowConstraints prefHeight="200" />
</rowConstraints>
<children>
<Label layoutX="10" layoutY="10" text="API接口" GridPane.columnIndex="0" GridPane.rowIndex="0">
<GridPane.margin>
<Insets bottom="10.0" top="5.0" />
</GridPane.margin>
</Label>
<ComboBox fx:id="comboBox_API" prefWidth="120" GridPane.columnIndex="1" GridPane.rowIndex="0">
<GridPane.margin>
<Insets bottom="10.0" top="5.0" />
</GridPane.margin>
</ComboBox>
<Button fx:id="mButton" mnemonicParsing="false" onAction="#onButtonClick" text="Button" GridPane.columnIndex="2" GridPane.rowIndex="1" />
<Label fx:id="mLabel" text="Label" GridPane.columnIndex="2" GridPane.rowIndex="2" />
</children>
</GridPane>
JavaFx在Main中获取Controller初始化相关配置和Fxml的更多相关文章
- .Net中获取打印机的相关信息
原文:.Net中获取打印机的相关信息 新项目中牵涉到对打印机的一些操作,最重要的莫过于获取打印机的状态,IP等信息,代码量不大,但是也是自己花了一点时间总结出来的,希望能帮助需要的朋友. Printe ...
- 在过滤器中获取在web.xml配置的初始化参数
在过滤器中获取在web.xml配置的初始化参数 例如 <filter> <filter-name>cross-origin</filter-name> < ...
- 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么
本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...
- ASP.NET MVC 中单独的JS文件中获取Controller中设定的值
1,在Controller中的Action 中将指定值写上. // // GET: /Home/ public ActionResult Index() ...
- Centos7中网络及设备相关配置
centos7中,不再赞成使用ifconfig工具,取而代之的是nmcli工具,服务管理也是以systemctl工具取代了service,这些之前版本的工具虽然在centos7中还可以继续使用,只是出 ...
- 【新手指南】Android Studio中应用App的相关配置
前言: 注意这是一个对于Android开发入门学习者而言的一个教程,因为自己平时很少使用Android进行原生应用的开发,对于使用Android Studio配置Android App应用的一些参数( ...
- 不用@Value从Spring的ApplicationContext中获取一个或全部配置
获取一个配置: applicationContext.getEnvironment().resolvePlaceholders("${propertyKey}"); // 方法1 ...
- Spring工具类 非spring管理环境中获取bean及环境配置
SpringUtils.java import org.springframework.beans.BeansException; import org.springframework.beans.f ...
- jquery中获取相邻元素相关的命令:next()、prev()和siblings()
jquery里我们要获取某个元素的相邻元素时,可以用到的命令有三个: next():用来获取下一个同辈元素. prev():用来获取上一个同辈元素. siblings():用来获取所有的同辈元素. 下 ...
随机推荐
- 【NOI2015】荷马史诗 - 哈夫曼树
题目描述 追逐影子的人,自己就是影子 ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛&g ...
- 设置与查看Linux系统中的环境变量
大家好,我是良许. 大家都知道,在 Linux 系统中,有环境变量和 Shell 变量这两种变量. 环境变量是在程序及其子程序中全局可用的,常常用来储存像默认的文本编辑器或者浏览器,以及可执行文件的路 ...
- JDK1.8源码学习-LinkedList
JDK1.8源码学习-LinkedList 目录 一.LinkedList简介 LinkedList是一个继承于AbstractSequentialList的双向链表,是可以在任意位置进行插入和移除操 ...
- github 加速方法
登录网址:https://github.com.ipaddress.com/codeload.github.com#ipinfo 更改hosts:
- linux下显示dd命令的进度
sudo dd if=/dev/zero of=/tmp/zero.img status=progresslinux下显示dd命令的进度:dd if=/dev/zero of=/tmp/zero.im ...
- Discovering Reinforcement Learning Algorithms
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! arXiv:2007.08794v1 [cs.LG] 17 Jul 2020 Abstract 强化学习(RL)算法根据经过多年研究手动发 ...
- Adversarial Attack Type I: Cheat Classifiers by Significant Changes
出于实现目的,翻译原文(侵删) Published in: IEEE Transactions on Pattern Analysis and Machine Intelligence (TPAMI ...
- CSS动画实例:3D立方体
CSS3支持3D转换,与3D转换有关的属性有: transform:向元素应用 2D或3D 转换. transform-origin:改变被转换元素的位置. transform-style:规定被嵌套 ...
- curl报错60的问题
使用curl发请post请求的时候,会遇到如下错误: curl: (60) SSL certificate problem: self signed certificate More details ...
- 使用手机安装Windows系统------DriveDroid
今天给大家推荐的软件是: DriveDroid 1.说来都是无奈,前一段时间,重装系统结果按完之后进不去系统,然后手贱又把U启动盘给弄坏了 2.本来想这下需要去找同学借个电脑了,然后就想手机可不可以啊 ...