JavaFX-如何访问FXML“对象" [英] Javafx - how to acces FXML "objects"

查看:166
本文介绍了JavaFX-如何访问FXML“对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的fxml文件中声明了一些JavaFX组件.

以及当按下按钮时如何获取字段的值(用户名,密码)? (执行登录).

  Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
  stage.setTitle("Login");
  stage.setScene(new Scene(root,400,300));
  stage.show();

或者这是完全错误的方法吗?

我的问题:

  1. 在fxml文件中声明所有字段是个好主意吗?
  2. 如何获取对象以获取值?
  3. 如果这是完全错误的方法,怎么办? (我想使用SceneBuilder)

https://hastebin.com/qexipogoma.xml <-我的FXML fie和我的控制器

解决方案

Scene scene = stage.getScene();
Button btn = (Button) scene.lookup("#myBtnID");
TextField txt = (TextField ) scene.lookup("#myTxtID");

您正在寻找:

txt.getText();

btn.setOnAction( lambda here );

文档:

按钮

TextField

以这种方式声明ID

<TextField fx:id="myTxtID" ...  />

I have some JavaFX components declared in my fxml file.

and how do I get the values of the fields (Username, Password) when the button is pressed? (To perform the login).

  Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
  stage.setTitle("Login");
  stage.setScene(new Scene(root,400,300));
  stage.show();

or is this the complete wrong way to do it?

My Questions:

  1. Is it a good idea to declare all fields in the fxml file?
  2. How do I get the objects to acces the Values?
  3. If its the complete wrong way, how can it be done? (I want to use scenebuilder)

EDIT : https://hastebin.com/qexipogoma.xml <- My FXML fie and my controller

解决方案

Scene scene = stage.getScene();
Button btn = (Button) scene.lookup("#myBtnID");
TextField txt = (TextField ) scene.lookup("#myTxtID");

you're looking for:

txt.getText();

and

btn.setOnAction( lambda here );

Documentation:

Button

TextField

EDIT: declare ids this way

<TextField fx:id="myTxtID" ...  />

这篇关于JavaFX-如何访问FXML“对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆