我的 fxml 文件中声明了一些 JavaFX 组件。
以及如何在按下按钮时获取字段(用户名、密码)的值? (执行登录)。
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
stage.setTitle("Login");
stage.setScene(new Scene(root,400,300));
stage.show();
或者这是完全错误的方法吗?
我的问题:
- 在 fxml 文件中声明所有字段是个好主意吗?
- 如何让对象访问值?
- 如果方法完全错误,怎么办? (我想使用 scenebuilder)
编辑: https://hastebin.com/qexipogoma.xml <- 我的 FXML 文件和我的 Controller
最佳答案
Scene scene = stage.getScene();
Button btn = (Button) scene.lookup("#myBtnID");
TextField txt = (TextField ) scene.lookup("#myTxtID");
您正在寻找:
txt.getText();
和
btn.setOnAction( lambda here );
文档:
编辑: 以这种方式声明 id
<TextField fx:id="myTxtID" ... />
关于Javafx - 如何访问 FXML "objects",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45864503/