将 TextBoxes 的文本与导致异常的字符串进行比较
Comparing the texts of TextBoxes with a string resulting in an exception
我正在使用带有 jfeonix
库的场景生成器制作登录屏幕。但是当我将文本字段的文本即 username
和 password
与字符串进行比较时,会导致异常。请帮我解决这个问题。如果用户名和密码匹配,它应该打印 "login successful".
FXML 代码:
<children>
<JFXTextField fx:id="useri" focusColor="WHITE" layoutX="84.0" layoutY="390.0" prefHeight="34.0" prefWidth="169.0" promptText="Username" style="-fx-prompt-text-fill: white; -fx-text-fill: white;" unFocusColor="#8a87e8">
</JFXTextField>
<JFXButton fx:id="loginbutton" layoutX="82.0" layoutY="495.0" onAction="#logina" prefHeight="34.0" prefWidth="170.0" style="-fx-background-color: blue; -fx-cursor: hand;" styleClass="header-section" stylesheets="@../../../../../Downloads/un.css" text="Login" textFill="#fffefe">
<font>
<Font name="System Bold" size="14.0" />
</font>
</JFXButton>
<JFXPasswordField fx:id="pass" focusColor="WHITE" layoutX="83.0" layoutY="439.0" prefHeight="34.0" prefWidth="170.0" promptText="Password" style="-fx-prompt-text-fill: white; -fx-text-fill: white;" unFocusColor="#688eff">
</JFXPasswordField>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="82.0" layoutY="53.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Untitled-2.jpg" />
</image>
</ImageView>
<ImageView fitHeight="26.0" fitWidth="26.0" layoutX="43.0" layoutY="443.0">
<image>
<Image url="@icons8_Lock_100px_1.png" />
</image>
</ImageView>
<ImageView fitHeight="26.0" fitWidth="26.0" layoutX="43.0" layoutY="394.0">
<image>
<Image url="@icons8_User_Menu_Male_100px_1.png" />
</image>
</ImageView>
<ImageView layoutX="114.0" layoutY="246.0">
<image>
<Image url="@icons8_Male_User_100px_1.png" />
</image>
</ImageView>
</children>
</AnchorPane>
<ImageView fx:id="closebutton" accessibleRole="BUTTON" fitHeight="34.0" fitWidth="70.0" layoutX="862.0" layoutY="14.0" onMouseClicked="#closebuttonaction" pickOnBounds="true" preserveRatio="true" style="-fx-cursor: hand;">
<image>
<Image url="@icons8_Close_Window_100px.png" />
</image>
</ImageView>
</children>
FXML 控制器代码:
package javafxapplication18;
import java.awt.Cursor;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import com.jfoenix.controls.*;
import javafx.event.EventHandler;
public class FXMLDocumentController implements Initializable {
@FXML
private Button closebutton;
private JFXTextField useri;
private JFXPasswordField pass;
private JFXButton loginbutton;
@FXML
private void handleButtonAction(ActionEvent event) {
}
@FXML
private void closebuttonaction(){
Platform.exit();
}
@FXML
private void logina(){
if (useri.getText().equals("admin")){
System.out.println("login successful");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
问题是您在 FXMLDocumentController 中初始化的字段上方缺少 @FXML 注释。所以异常的原因是 "useri" 没有初始化,我猜这个异常是 NullPointerException
public class FXMLDocumentController implements Initializable {
@FXML
private Button closebutton;
@FXML
private JFXTextField useri;
@FXML
private JFXPasswordField pass;
@FXML
private JFXButton loginbutton;
我正在使用带有 jfeonix
库的场景生成器制作登录屏幕。但是当我将文本字段的文本即 username
和 password
与字符串进行比较时,会导致异常。请帮我解决这个问题。如果用户名和密码匹配,它应该打印 "login successful".
FXML 代码:
<children>
<JFXTextField fx:id="useri" focusColor="WHITE" layoutX="84.0" layoutY="390.0" prefHeight="34.0" prefWidth="169.0" promptText="Username" style="-fx-prompt-text-fill: white; -fx-text-fill: white;" unFocusColor="#8a87e8">
</JFXTextField>
<JFXButton fx:id="loginbutton" layoutX="82.0" layoutY="495.0" onAction="#logina" prefHeight="34.0" prefWidth="170.0" style="-fx-background-color: blue; -fx-cursor: hand;" styleClass="header-section" stylesheets="@../../../../../Downloads/un.css" text="Login" textFill="#fffefe">
<font>
<Font name="System Bold" size="14.0" />
</font>
</JFXButton>
<JFXPasswordField fx:id="pass" focusColor="WHITE" layoutX="83.0" layoutY="439.0" prefHeight="34.0" prefWidth="170.0" promptText="Password" style="-fx-prompt-text-fill: white; -fx-text-fill: white;" unFocusColor="#688eff">
</JFXPasswordField>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="82.0" layoutY="53.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@Untitled-2.jpg" />
</image>
</ImageView>
<ImageView fitHeight="26.0" fitWidth="26.0" layoutX="43.0" layoutY="443.0">
<image>
<Image url="@icons8_Lock_100px_1.png" />
</image>
</ImageView>
<ImageView fitHeight="26.0" fitWidth="26.0" layoutX="43.0" layoutY="394.0">
<image>
<Image url="@icons8_User_Menu_Male_100px_1.png" />
</image>
</ImageView>
<ImageView layoutX="114.0" layoutY="246.0">
<image>
<Image url="@icons8_Male_User_100px_1.png" />
</image>
</ImageView>
</children>
</AnchorPane>
<ImageView fx:id="closebutton" accessibleRole="BUTTON" fitHeight="34.0" fitWidth="70.0" layoutX="862.0" layoutY="14.0" onMouseClicked="#closebuttonaction" pickOnBounds="true" preserveRatio="true" style="-fx-cursor: hand;">
<image>
<Image url="@icons8_Close_Window_100px.png" />
</image>
</ImageView>
</children>
FXML 控制器代码:
package javafxapplication18;
import java.awt.Cursor;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import com.jfoenix.controls.*;
import javafx.event.EventHandler;
public class FXMLDocumentController implements Initializable {
@FXML
private Button closebutton;
private JFXTextField useri;
private JFXPasswordField pass;
private JFXButton loginbutton;
@FXML
private void handleButtonAction(ActionEvent event) {
}
@FXML
private void closebuttonaction(){
Platform.exit();
}
@FXML
private void logina(){
if (useri.getText().equals("admin")){
System.out.println("login successful");
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
问题是您在 FXMLDocumentController 中初始化的字段上方缺少 @FXML 注释。所以异常的原因是 "useri" 没有初始化,我猜这个异常是 NullPointerException
public class FXMLDocumentController implements Initializable {
@FXML
private Button closebutton;
@FXML
private JFXTextField useri;
@FXML
private JFXPasswordField pass;
@FXML
private JFXButton loginbutton;