在场景构建器中,我有一个密码字段,其中包含fx:id passwordBox以及我所拥有的相应控制器类
@FXML private static PasswordField passwordBox = new PasswordField();
我还试过
@FXML private static PasswordField passwordBox;
当我运行程序时,密码字段中的字母是纯文本 . 当我在场景构建器中预览窗口时,会发生同样的事情 . 密码字段是一个PasswordField,所以我没有把它误认为是TextField .
我能做什么?
编辑:FXML文件
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<AnchorPane fx:id="mainAnchor" opacity="1.0" prefHeight="200.0" prefWidth="408.0000999999975" styleClass="back" xmlns:fx="http://javafx.com/fxml" fx:controller="application.MainController">
<!-- TODO Add Nodes -->
<children>
<Label fx:id="serverIPLbl" layoutX="14.0" layoutY="28.0" prefWidth="175.0" text="Server IP">
<font>
<Font name="Segoe UI" size="12.0" fx:id="x1" />
</font>
</Label>
<TextField fx:id="serverIPBox" layoutX="14.0" layoutY="50.0" prefWidth="175.0" />
<Label fx:id="portLbl" font="$x1" layoutX="215.0" layoutY="28.0" prefWidth="175.0" text="Port" />
<TextField fx:id="portBox" layoutX="215.0" layoutY="50.0" prefWidth="175.0" />
<Label fx:id="passwordLbl" font="$x1" layoutX="14.0" layoutY="81.0" prefWidth="175.0" text="Server Password" />
<ImageView fx:id="settingsButton" fitHeight="23.0" fitWidth="23.0" layoutX="14.0" layoutY="137.0" onMouseClicked="#settingsClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/gear.png" />
</image>
</ImageView>
<Button id="startServer" fx:id="connectButton" layoutX="14.0" layoutY="167.0" mnemonicParsing="false" onAction="#connectClicked" prefHeight="22.0" prefWidth="376.0" text="Connect" />
<Label fx:id="usernameLbl" font="$x1" layoutX="215.0" layoutY="81.0" prefWidth="175.0" text="Username" />
<TextField id="serverIPBox" fx:id="usernameBox" layoutX="215.0" layoutY="103.0" prefWidth="175.0" promptText="a-z A-Z 0-9 _ - chars allowed" />
<ImageView id="favoriteButton" fx:id="favoritesButton" fitHeight="23.0" fitWidth="23.0" layoutX="55.0" layoutY="137.0" onMouseClicked="#favoritesClicked" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/favorite.png" />
</image>
</ImageView>
<PasswordField fx:id="passwordBox" layoutX="14.0" layoutY="103.0" prefWidth="175.0" promptText="Optional" />
</children>
<stylesheets>
<URL value="@style.css" />
</stylesheets>
</AnchorPane>
1 回答
Issues with your code
@FXML
是控制器实例的注入注释,不应将其与static
成员或使用new
关键字初始化的成员一起使用 .相反,控制器中的定义应该是:
Additional considerations which may or may not apply in your case
控制器实例应始终通过新的FXMLLoader() . load(...)调用创建(不要通过控制器本身的
new
关键字创建,除非您随后使用loader.setController()) .fxml文件必须指定密码字段的名称,该名称与控制器中的名称相匹配(在您的情况下,此名称为
passwordBox
) .Example
GatewayApplication.java
MainController.java
由SceneBuilder
View | Show Sample Skeleton
生成 .style.css文件
passport.fxml
示例程序UI
示例程序输出
在密码字段中输入魔术作品“xyzzy”并按下连接后,程序将从字段中提取密码并将其打印到控制台:
使用的测试系统是运行Java 8b121的OS X 10.9 .
在将来的这类问题中,您可能需要提供minimal, complete, tested and readable example . 所有问题都不需要这样的样本,但肯定会对此有所帮助 .