我正在尝试为按钮创建一个事件,该按钮捕获在TextField和PasswordField Control中输入的文本。我知道这与事件处理有关,但不知道如何实施。您将在我的代码中看到我从称为Addhandler开始的课程。我还试图构建一个将迎接并包括用户并传递在标记为空的新标签(我的窗格的位置4(中的字符串。
。import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.text.Text;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
public class UserAndPass extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane and set its properties
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
// Place nodes in the pane
pane.add(new Label("Please enter your username and password: "), 0, 0);
pane.add(new Label("Username: "), 0, 1);
pane.add(new TextField(), 1, 1);
pane.add(new Label("Password: "), 0, 2);
pane.add(new TextField(), 1, 2);
Button btAdd = new Button("Enter");
pane.add(btAdd, 1, 3);
// Create and register the handler
btAdd.setOnAction(new AddHandler());
GridPane.setHalignment(btAdd, HPos.RIGHT);
pane.add(new Label(""), 0, 4);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Login Display"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
class AddHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
AddHandler.enter();
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
访问处理程序的textfields和标签,使其成为字段。
public class InnerClasssTest extends Application {
private TextField usernameField = new TextField();
private TextField passwordField = new TextField();
private Label greetingLabel = new Label("");
pane.add(new Label("Please enter your username and password: "), 0, 0);
pane.add(new Label("Username: "), 0, 1);
pane.add(usernameField, 1, 1);
pane.add(new Label("Password: "), 0, 2);
pane.add(passwordField, 1, 2);
Button btAdd = new Button("Enter");
pane.add(btAdd, 1, 3);
// Create and register the handler
btAdd.setOnAction(new AddHandler());
GridPane.setHalignment(btAdd, HPos.RIGHT);
pane.add(greetingLabel, 0, 4);
class AddHandler implements EventHandler<ActionEvent> {
@Override // Override the handle method
public void handle(ActionEvent e) {
greetingLabel.setText(String.format(
"Hello %s@%s.", usernameField.getText(), passwordField.getText()));
}
}
编辑:对不起,我误读了它,您需要首先创建字段,例如 @monolith52 alldready已准备就绪
Button randomButton = new Button("Click Me");
您可以使用lambda表达式来完成您想要的事情。
这里是一行: randombutton .setonaction(e-> 在这里做您想要的(;
和多行:
randomButton .setOnAction(e -> {
*Do here what you want*
});
然后,您必须简单地从您的字段中获取字符串 with:
userNameField.getText()
和
passwordField.getText()