以下代码是在JavaFX中实现的登录验证代码。但是我收到很多错误,不知道为什么......请检查以下2个代码 textField Id:用户 passwordField Id:传递
sample.fxml(使用SceneBuilder)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<AnchorPane id="LoginAnchor" prefHeight="318.0" prefWidth="443.0" styleClass="background" stylesheets="@LoginStylesheet.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox alignment="CENTER" layoutX="32.0" layoutY="13.0" prefHeight="108.0" prefWidth="247.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="CENTER">
<children>
<Label id="label" text="Username:">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<TextField id="user" />
</children>
</HBox>
<HBox alignment="CENTER">
<children>
<Label text="Password:">
<HBox.margin>
<Insets right="11.0" />
</HBox.margin>
</Label>
<PasswordField id="pass">
<HBox.margin>
<Insets left="2.0" />
</HBox.margin>
</PasswordField>
</children>
<VBox.margin>
<Insets bottom="10.0" top="10.0" />
</VBox.margin>
</HBox>
<Button fx:id="loginButton" alignment="CENTER" mnemonicParsing="false" onAction="#loginButtonClicked" text="Log In" />
</children>
</VBox>
</children>
</AnchorPane>
controller.java
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private TextField user;
@FXML
private PasswordField pass;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML
public void loginButtonClicked(ActionEvent event) {
if (user.getText().equals("test") && pass.getText().equals("password")) {
System.out.println("log In" + user.getText());
} else {
System.out.println("not Login");
}
}
}