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():用来获取所有的同辈元素. 下 ...
随机推荐
- 初 识 eric4
下图展示了,如何使用eric4 新建工程,创建窗体,编译工程,运行工程这几个过程
- servlet的生命周期和工作原理介绍
一.servlet生命周期 Servlet生命周期分为三个阶段: 1)初始化阶段: 调用init()方法 2)响应客户请求阶段:调用service()方法 3)终止阶段:调用destroy()方法 T ...
- Java并发---concurrent包
一.包的结构层次 其中包含了两个子包atomic和locks,另外字concurrent下的阻塞队列以及executor,这些就是concurrent包中的精华.而这些类的实现主要是依赖于volati ...
- Linux bpytop工具介绍
一.工具简介: Easy to use, with a game inspired menu system. Full mouse support, all buttons with a highli ...
- 哇,ElasticSearch多字段权重排序居然可以这么玩
背景 读者提问:ES 的权重排序有没有示列,参考参考? 刚好之前也稍微接触过,于是写了这篇文章,可以简单参考下. 在很多复杂的业务场景下,排序的规则会比较复杂,单一的降序,升序无法满足日常需求.不过 ...
- vue混入mixins时注意的问题
mixin.js - 方式一:导出对象 const mixin = { mounted () { console.log('fffffffffffff') }, methods: { } } expo ...
- idea如何安装插件
原文地址:https://jingyan.baidu.com/article/215817f742a61c1eda142329.html 1.首先打开idea界面,然后 按住快捷键ctrl+shift ...
- 力扣Leetcode 33. 搜索旋转排序数组
33. 搜索旋转排序数组 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值, ...
- [C#.NET 拾遗补漏]08:强大的LINQ
大家好,这是 [C#.NET 拾遗补漏] 系列的第 08 篇文章,今天讲 C# 强大的 LINQ 查询.LINQ 是我最喜欢的 C# 语言特性之一. LINQ 是 Language INtegrate ...
- Data Vault玩转数据仓库(三)
在Data Vault 2.0版本里,其不只是针对数据仓库的建模,同时也包含了架构,方法论以及实现.这篇挑几个概念,附上我个人对其的理解.同时也把这个系列的名字改成<Data Vault玩转数据 ...