如何请求某个字段的焦点?

问题描述 投票:0回答:1

我正在为带有证书系统的智能卡令牌管理开发登录对话框。下面,我添加了示例代码来演示该问题。如果启动应用程序,我首先会看到一个登录对话框,可以在其中输入 root 用户的密码。第一个对话框仍然允许使用 Tab 键从一个字段导航到另一个字段。但如果我单击“确定”按钮,那么焦点仍然在“确定”按钮上。有什么想法如何解决吗?焦点需要放在第一个密码字段上。

打包 Java 源文件夹

package loginregister.demologin;

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

import java.io.IOException;

public class LoginRegisterApp extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(LoginRegisterApp.class.getResource("LoginRegisterDialog.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("Login Register Dialog");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

package loginregister.demologin;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;

public class LoginRegisterController implements Initializable {
    public TextField tokenName;
    public PasswordField newPin;
    @FXML
    private Button btnLogin;
    private Stage primaryStage;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        this.primaryStage = new Stage();
    }

    @FXML
    void handleCancel(ActionEvent event) {
        Node node = (Node) event.getSource();
        this.primaryStage = (Stage) node.getScene().getWindow();
        this.primaryStage.hide();
    }

    public void switchForm(ActionEvent event) {
        if (event.getSource() == btnLogin) {
            tokenName.setText("admin1");
        }
    }
}

将loginregister.demologin打包到资源文件夹中

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<StackPane xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="200.0" style="-fx-background-color: white;" fx:controller="loginregister.demologin.LoginRegisterController">
   <children>
      <VBox fx:id="hasAllPanes" alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="200.0">
         <children>
            <AnchorPane fx:id="outerAnchorPane" prefHeight="300.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
               <children>
                  <AnchorPane fx:id="loginForm" layoutX="2.0" layoutY="64.0" prefHeight="150.0" prefWidth="200.0">
                     <children>
                        <TextField fx:id="tokenName" disable="true" editable="false" layoutX="14.0" layoutY="14.0" prefHeight="33.0" prefWidth="180.0" promptText="root" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" />
                        <PasswordField fx:id="newPin" layoutX="10.0" layoutY="61.0" prefHeight="33.0" prefWidth="180.0" promptText="Enter New PIN" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" />
                        <PasswordField layoutX="9.0" layoutY="110.0" prefHeight="33.0" prefWidth="180.0" promptText="Re-enter New PIN" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" />
                     </children>
                  </AnchorPane>
                  <HBox fx:id="hBoxOkCancel" alignment="CENTER" layoutX="10.0" layoutY="255.0" prefHeight="33.0" prefWidth="180.0" AnchorPane.bottomAnchor="11.913288912176938" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
                     <children>
                        <Button fx:id="btnLogin" graphicTextGap="10.0" mnemonicParsing="false" onAction="#switchForm" prefHeight="33.0" prefWidth="80.0" text="OK" HBox.hgrow="ALWAYS">
                           <font>
                              <Font size="12.0" />
                           </font>
                        </Button>
                        <Region prefHeight="33.0" prefWidth="30.0" HBox.hgrow="ALWAYS" />
                        <Button fx:id="btnCancel" graphicTextGap="10.0" mnemonicParsing="false" onAction="#handleCancel" prefHeight="33.0" prefWidth="80.0" text="Exit" HBox.hgrow="ALWAYS" />
                     </children>
                  </HBox>
               </children>
            </AnchorPane>
         </children>
      </VBox>
   </children>
</StackPane>
javafx fxml
1个回答
0
投票

在创建问题的过程中,我自己解决了这个问题。我添加了这一行

newPin.requestFocus();

“switchForm”方法需要如下所示:

public void switchForm(ActionEvent event) {
        if (event.getSource() == btnLogin) {
            tokenName.setText("admin1");
            newPin.requestFocus();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.