我正在尝试为按钮创建一个事件,该事件捕获在TextField和PasswordField控件中输入的文本。我知道它与事件处理有关,但不知道如何实现它。您将在我的代码中看到我使用名为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);
}
}
答案 0 :(得分:1)
要从处理程序访问文本字段和标签,请将它们设为字段。
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()));
}
}
答案 1 :(得分:0)
编辑:抱歉,我误读了,您需要先创建字段,如@ monolith52 allready said
Button randomButton = new Button("Click Me");
您可以使用Lambda表达式来执行您想要的操作。
此处为单行: randomButton .setOnAction(e - &gt; 在这里做你想要的事情);
并且作为多行:
randomButton .setOnAction(e -> {
*Do here what you want*
});
然后,您必须简单地从字段获取字符串:
userNameField.getText()
和
passwordField.getText()