gpt4 book ai didi

java - 处理 JavaFX 事件,例如从文本字段控件捕获文本

转载 作者:行者123 更新时间:2023-12-02 13:15:34 25 4
gpt4 key购买 nike

我正在尝试为捕获在 TextField 和 PasswordField 控件中输入的文本的按钮创建一个事件。我知道它与事件处理有关,但不知道如何实现它。您将在我的代码中看到我开始使用的名为 AddHandler 的类。我还尝试构建一个字符串,该字符串将问候并包含用户,并在标记为空的新标签中传递问候标签控件( Pane 的位置 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);
}
}

最佳答案

要从处理程序访问文本字段和标签,请将它们设为字段。

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()));
}
}

关于java - 处理 JavaFX 事件,例如从文本字段控件捕获文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com