使用 onAction 属性 返回按钮文本

Returning button text with the onAction property

我有一个方法,它接收一个名称列表,然后为列表中的每个名称创建按钮。我想return字符串形式的名称或列表中的哪个数字被点击,但我觉得很难做到

public static String display(List<String> names) {
    Stage window = new Stage();
    window.setTitle("title");
    GridPane layout = new GridPane();

    for (int i = 0; i < names.size(); i++) {
        Button b = new Button(names.get(i);
        GridPane.setConstraints(b, 0, i);
        b.setOnAction(e - > {
            // return names.get(i);
            // or the number in the list
            // or the text of the button
        });

        layout.getChildren().add(b);
    }

    Scene scene = new Scene(layout, 300, 250);
    window.setScene(scene);
    window.showAndWait();

    return null;
}

我尝试了什么:

    String s = "";
    b.setOnAction(e - > {
        s = b.getText();
    });
    
    return s;

但我收到以下错误:local variable is defined in an enclosing scope must be final or effective final

这里基本上有两种选择。

选项 1:

System.out.println(b.getText());

选项 2:我喜欢什么

Button tempButton = ((Button)e.getSource());
System.out.println(tempButton.getText());

完整代码:我建议不要这样做!我认为您应该使用 AlertDialog 来完成此操作!

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        List<String> names = new ArrayList();
        names.add("john");
        names.add("Kim");
        
        Button button = new Button("List of Names Menu");
        button.setOnAction((e) -> {
            System.out.println(display(names));
        });

        StackPane layout = new StackPane();
        layout.getChildren().add(button);

        Scene scene = new Scene(layout, 300, 250);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

    public String display(List <String> names) {
        Label label = new Label();

        Stage window = new Stage();
        window.setTitle("title");
        GridPane layout = new GridPane();

        for (int i = 0; i < names.size(); i++) {
            Button b = new Button(names.get(i));
            GridPane.setConstraints(b, 0, i);
            b.setOnAction((e) -> {
                label.setText(b.getText());
                window.close();
            });
            layout.getChildren().add(b);
        }
        Scene scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.showAndWait();

        return label.getText();
    }
}

为什么不做

public static String display(List<String> names) {

  StringBuilder result = new StringBuilder();

  Stage window = new Stage();
  window.setTitle("title");
  GridPane layout = new GridPane();

  for (int i = 0; i < names.size(); i++) {
    String name = names.get(i);
    Button b = new Button(name); GridPane.setConstraints(b, 0, i);
    b.setOnAction(e -> {
        result.append(name);
        window.close();
    });
    layout.getChildren().add(b);
  }

  Scene scene = new Scene(layout, 300, 250);
  window.setScene(scene);
  window.showAndWait();

  return result.toString();

}

如果你使用 VBox,这看起来更自然,你可以使代码更清晰(因为你不需要列表索引):

public static String display(List<String> names) {

  StringBuilder result = new StringBuilder();

  Stage window = new Stage();
  window.setTitle("title");
  VBox layout = new VBox();

  for (String name : names) {
    Button b = new Button(name); 
    b.setOnAction(e -> {
        result.append(name);
        window.close();
    });
    layout.getChildren().add(b);
  }
  Scene scene = new Scene(layout, 300, 250);
  window.setScene(scene);
  window.showAndWait();

  return result.toString();    
}