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的更多相关文章

  1. .Net中获取打印机的相关信息

    原文:.Net中获取打印机的相关信息 新项目中牵涉到对打印机的一些操作,最重要的莫过于获取打印机的状态,IP等信息,代码量不大,但是也是自己花了一点时间总结出来的,希望能帮助需要的朋友. Printe ...

  2. 在过滤器中获取在web.xml配置的初始化参数

    在过滤器中获取在web.xml配置的初始化参数   例如 <filter> <filter-name>cross-origin</filter-name> < ...

  3. 【spring boot logback】日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么

    本篇 将针对[日志使用自定义的logback-spring.xml文件后,application.properties中关于日志的相关配置还会起作用么]这一个主题进行探索. 这个测试项目是根据[spr ...

  4. ASP.NET MVC 中单独的JS文件中获取Controller中设定的值

    1,在Controller中的Action 中将指定值写上.       //       // GET: /Home/       public ActionResult Index()       ...

  5. Centos7中网络及设备相关配置

    centos7中,不再赞成使用ifconfig工具,取而代之的是nmcli工具,服务管理也是以systemctl工具取代了service,这些之前版本的工具虽然在centos7中还可以继续使用,只是出 ...

  6. 【新手指南】Android Studio中应用App的相关配置

    前言: 注意这是一个对于Android开发入门学习者而言的一个教程,因为自己平时很少使用Android进行原生应用的开发,对于使用Android Studio配置Android App应用的一些参数( ...

  7. 不用@Value从Spring的ApplicationContext中获取一个或全部配置

    获取一个配置: applicationContext.getEnvironment().resolvePlaceholders("${propertyKey}"); // 方法1 ...

  8. Spring工具类 非spring管理环境中获取bean及环境配置

    SpringUtils.java import org.springframework.beans.BeansException; import org.springframework.beans.f ...

  9. jquery中获取相邻元素相关的命令:next()、prev()和siblings()

    jquery里我们要获取某个元素的相邻元素时,可以用到的命令有三个: next():用来获取下一个同辈元素. prev():用来获取上一个同辈元素. siblings():用来获取所有的同辈元素. 下 ...

随机推荐

  1. es数据库基本操作

    1.es建立索引: curl -XPUT 'http://10.xx.xx.xx:9200/索引名称' 2.es查询所有索引: curl -XGET 'http://10.xx.xx.xx:9200/ ...

  2. 4.设置静态IP

    由于Ubuntu重启之后,ip很容易改变,可以用以下方式固定ip地址 1.设置ip地址 vi /etc/network/interface # The loopback network interfa ...

  3. js中的寄生组合继承

    function inheritProperty(subType, superType) { function F(){} F.prototype = superType.prototype; sup ...

  4. 手把手教Linux驱动3-之字符设备架构详解,有这篇就够了

    一.Linux设备分类 Linux系统为了管理方便,将设备分成三种基本类型: 字符设备 块设备 网络设备 字符设备: 字符(char)设备是个能够像字节流(类似文件)一样被访问的设备,由字符设备驱动程 ...

  5. 牛客网PAT练兵场-统计同成绩学生

    题解:开100的数组,进行存储人数,方便查询 题目地址:https://www.nowcoder.com/questionTerminal/3df4810cc0664b8bb848d785f68f7c ...

  6. VMDNAMD命令规则(转载)

    输出体系的整个带电量:measure sumweights $all weight charge 给PDB文件设置周期边界条件:pbc set {54 54 24 } -all 将此晶胞内原子脱除周期 ...

  7. ASP.NET Core3.1使用IdentityServer4中间件系列随笔(四):创建使用[ResourceOwnerPassword-资源所有者密码凭证]授权模式的客户端

    配套源码:https://gitee.com/jardeng/IdentitySolution 本篇将创建使用[ResourceOwnerPassword-资源所有者密码凭证]授权模式的客户端,来对受 ...

  8. FormData格式的数据

    向服务器提交的是FormData格式的数据 || 必须添加以下两个配置项 contentType:false, processData:false,

  9. 使用 C# 捕获进程输出

    使用 C# 捕获进程输出 Intro 很多时候我们可能会需要执行一段命令获取一个输出,遇到的比较典型的就是之前我们需要用 FFMpeg 实现视频的编码压缩水印等一系列操作,当时使用的是 FFMpegC ...

  10. Node.js调试相关

    如何进行Nodejs性能分析? nodejs性能最重要的两个部分:CPU耗时查看和内存泄漏排查 一,CPU相关 主要思路是两个:借助第三方的工具,以及借助v8自带的性能分析工具 借助第三方的工具 主要 ...