当前位置: 首页 > 知识库问答 >
问题:

JavaFX密码字段不工作

米项禹
2023-03-14

编辑: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个答案

虞正业
2023-03-14

代码的问题

@fxml是控制器实例的注入注释,您不应该将它与static成员或使用new关键字初始化的成员结合使用。

相反,您的控制器中的定义应该是:

@FXML private PasswordField passwordBox;
    null
package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;

public class GatewayApplication extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("passport.fxml"));
        AnchorPane layout = loader.load();
        stage.setScene(new Scene(layout));
        stage.show();
    }
}
package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;


public class MainController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button connectButton;

    @FXML
    private ImageView favoritesButton;

    @FXML
    private AnchorPane mainAnchor;

    @FXML
    private PasswordField passwordBox;

    @FXML
    private Label passwordLbl;

    @FXML
    private TextField portBox;

    @FXML
    private Label portLbl;

    @FXML
    private TextField serverIPBox;

    @FXML
    private Label serverIPLbl;

    @FXML
    private ImageView settingsButton;

    @FXML
    private TextField usernameBox;

    @FXML
    private Label usernameLbl;


    @FXML
    void connectClicked(ActionEvent event) {
        System.out.println("password = " + passwordBox.getText());
    }

    @FXML
    void favoritesClicked(MouseEvent event) {
    }

    @FXML
    void settingsClicked(MouseEvent event) {
    }

    @FXML
    void initialize() {
        assert connectButton != null : "fx:id=\"connectButton\" was not injected: check your FXML file 'passport.fxml'.";
        assert favoritesButton != null : "fx:id=\"favoritesButton\" was not injected: check your FXML file 'passport.fxml'.";
        assert mainAnchor != null : "fx:id=\"mainAnchor\" was not injected: check your FXML file 'passport.fxml'.";
        assert passwordBox != null : "fx:id=\"passwordBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert passwordLbl != null : "fx:id=\"passwordLbl\" was not injected: check your FXML file 'passport.fxml'.";
        assert portBox != null : "fx:id=\"portBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert portLbl != null : "fx:id=\"portLbl\" was not injected: check your FXML file 'passport.fxml'.";
        assert serverIPBox != null : "fx:id=\"serverIPBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert serverIPLbl != null : "fx:id=\"serverIPLbl\" was not injected: check your FXML file 'passport.fxml'.";
        assert settingsButton != null : "fx:id=\"settingsButton\" was not injected: check your FXML file 'passport.fxml'.";
        assert usernameBox != null : "fx:id=\"usernameBox\" was not injected: check your FXML file 'passport.fxml'.";
        assert usernameLbl != null : "fx:id=\"usernameLbl\" was not injected: check your FXML file 'passport.fxml'.";
    }

}
.root {
    -fx-background-color: cornsilk;
}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import java.net.URL?>
<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">
    <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="http://icons.iconarchive.com/icons/hopstarter/soft-scraps/24/Gear-icon.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="http://icons.iconarchive.com/icons/hopstarter/soft-scraps/24/Button-Favorite-icon.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>

示例程序界面

示例程序输出

在密码字段中输入魔术作品“xyzzy”并按connect键后,程序从字段中提取密码并将其打印到控制台:

password = xyzzy
 类似资料:
  • 主要内容:创建密码字段,示例用于密码输入。用户键入的字符通过显示回显字符串被隐藏。 创建密码字段 以下代码使用来自类的默认构造函数创建一个密码字段,然后为密码字段设置提示消息文本。 提示消息在字段中显示为灰色文本,并为用户提供该字段是什么的提示,而不使用标签控件。 类有方法来为控件设置文本字符串。对于密码字段,指定的字符串由回显字符隐藏。默认情况下,回显字符是一个点(或是星号)。 密码字段中的值可以通过方法获取。 示例 密码

  • 本文向大家介绍如何验证在JavaFX密码字段中输入的密码?,包括了如何验证在JavaFX密码字段中输入的密码?的使用技巧和注意事项,需要的朋友参考一下 文本字段接受并显示文本。在最新版本的JavaFX中,它仅接受一行。 与文本字段类似,密码字段接受文本,但是不显示给定的文本,而是通过显示回显字符串来隐藏键入的字符。 在JavaFX中,javafx.scene.control.PasswordFie

  • User对象 加密方法 项目中加密字段比较多,如上User对象密码和手机号的加密密钥不一样 希望一个密钥对应一个AES,不想频繁的创建AES对象,如何封装这个逻辑

  • 我目前正在学习Spring Boot framework,并试图按照baledung页面上的指南创建一个自定义字段匹配验证器。 我的代码实际上和上面的页面一样,唯一不同的是我用BCryptPasswordEncoder编码了密码。 这是我的用户类。 控制器: 注册用户方法: 我收到了以下错误: 我认为编码的密码正在与未编码的验证密码进行比较。这就是为什么我收到这个错误或?我怎么才能修好它?如果我也

  • 问题内容: 我正在尝试创建一个模型,在其中可以存储其他应用程序的用户名和密码。如何在Django中设置密码字段,使其在admin中不是纯文本格式?提前致谢。 问题答案: 正如@mlissner所建议的那样,该模型是一个不错的选择。如果你查看源代码,则会看到该d字段是。 该User模型也有一种set_password方法。 你可以从此方法中获得一些有关创建和保存密码的线索。