按下按钮 FXML 后从 Textfield 获取文本
Get text from Textfield after pressing button FXML
我试图在按下按钮后获取文本字段中的文本。
程序运行良好,fxml 加载并且程序运行,但每当我按下提交按钮时,都会打印一个空行,而不是文本字段中的内容。
这是 Java 和 FXML 代码:
Java 文件:
package me.Excursion.UI;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Login extends Application {
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root, 1200, 800);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@FXML
private void loginSubmitClick(ActionEvent e){
TextField Username = new TextField();
System.out.println(Username.getText());
}
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.Excursion.UI.Login">
<children>
<ImageView fitHeight="822.0" fitWidth="1200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@pictures/Blurred Sea.jpg" />
</image>
</ImageView>
<AnchorPane layoutX="50.0" layoutY="40.0" prefHeight="720.0" prefWidth="520.0" style="-fx-opacity: 0.75; -fx-background-color: white;" />
<AnchorPane layoutX="84.0" layoutY="72.0" prefHeight="656.0" prefWidth="450.0">
<children>
<TextField fx:id="Username" layoutX="102.0" layoutY="255.0" prefHeight="26.0" prefWidth="248.0" promptText="Username" />
<PasswordField fx:id="Password" layoutX="101.0" layoutY="339.0" prefHeight="26.0" prefWidth="248.0" promptText="Password" />
<ImageView fitHeight="208.0" fitWidth="226.0" layoutX="121.0" layoutY="30.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@pictures/54330b43-40d1-419a-a4b7-e04805c54560.png" />
</image>
</ImageView>
<Button layoutX="145.0" layoutY="438.0" mnemonicParsing="false" onAction="#loginSubmitClick" prefHeight="40.0" prefWidth="160.0" style="-fx-background-color: #083B66;" text="Submit" />
<Hyperlink layoutX="166.0" layoutY="505.0" style="-fx-underline: true;" text="Forgot Password?" />
</children>
</AnchorPane>
</children>
</AnchorPane>
不是创建新的 TextField,而是声明一个 class 变量
@FXML
private TextField Username;
这会起作用
我试图在按下按钮后获取文本字段中的文本。
程序运行良好,fxml 加载并且程序运行,但每当我按下提交按钮时,都会打印一个空行,而不是文本字段中的内容。
这是 Java 和 FXML 代码:
Java 文件:
package me.Excursion.UI;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Login extends Application {
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root, 1200, 800);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
@FXML
private void loginSubmitClick(ActionEvent e){
TextField Username = new TextField();
System.out.println(Username.getText());
}
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Hyperlink?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="me.Excursion.UI.Login">
<children>
<ImageView fitHeight="822.0" fitWidth="1200.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@pictures/Blurred Sea.jpg" />
</image>
</ImageView>
<AnchorPane layoutX="50.0" layoutY="40.0" prefHeight="720.0" prefWidth="520.0" style="-fx-opacity: 0.75; -fx-background-color: white;" />
<AnchorPane layoutX="84.0" layoutY="72.0" prefHeight="656.0" prefWidth="450.0">
<children>
<TextField fx:id="Username" layoutX="102.0" layoutY="255.0" prefHeight="26.0" prefWidth="248.0" promptText="Username" />
<PasswordField fx:id="Password" layoutX="101.0" layoutY="339.0" prefHeight="26.0" prefWidth="248.0" promptText="Password" />
<ImageView fitHeight="208.0" fitWidth="226.0" layoutX="121.0" layoutY="30.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@pictures/54330b43-40d1-419a-a4b7-e04805c54560.png" />
</image>
</ImageView>
<Button layoutX="145.0" layoutY="438.0" mnemonicParsing="false" onAction="#loginSubmitClick" prefHeight="40.0" prefWidth="160.0" style="-fx-background-color: #083B66;" text="Submit" />
<Hyperlink layoutX="166.0" layoutY="505.0" style="-fx-underline: true;" text="Forgot Password?" />
</children>
</AnchorPane>
</children>
</AnchorPane>
不是创建新的 TextField,而是声明一个 class 变量
@FXML
private TextField Username;
这会起作用