通过onAction属性返回按钮文本

huangapple go评论70阅读模式
英文:

Returning button text with the onAction property

问题

public static String display(List<String> names) {
    Stage window = new Stage();
    window.setTitle("title");
    GridPane layout = new GridPane();
    final String[] clickedName = {""}; // Declare an array to hold the clicked name

    for (int i = 0; i < names.size(); i++) {
        Button b = new Button(names.get(i));
        GridPane.setConstraints(b, 0, i);
        b.setOnAction(e -> {
            clickedName[0] = names.get(i); // Store the clicked name in the array
            window.close(); // Close the window when a button is clicked
        });

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

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

    return clickedName[0]; // Return the clicked name
}
英文:

I have a method that takes in a list of names and then create buttons for each name on the list. I want to return the name in the form of a string or which number in the list was clicked, but I find it hard to do

public static String display(List&lt;String&gt; names) {
    Stage window = new Stage();
    window.setTitle(&quot;title&quot;);
    GridPane layout = new GridPane();

    for (int i = 0; i &lt; names.size(); i++) {
        Button b = new Button(names.get(i);
        GridPane.setConstraints(b, 0, i);
        b.setOnAction(e - &gt; {
            // 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;
}

What I tried:

    String s = &quot;&quot;;
    b.setOnAction(e - &gt; {
        s = b.getText();
    });
    
    return s;

But I get the following error: local variable is defined in an enclosing scope must be final or effective final.

答案1

得分: 2

为什么不这样做

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

Why not do

public static String display(List&lt;String&gt; names) {
StringBuilder result = new StringBuilder();
Stage window = new Stage();
window.setTitle(&quot;title&quot;);
GridPane layout = new GridPane();
for (int i = 0; i &lt; names.size(); i++) {
String name = names.get(i);
Button b = new Button(name); GridPane.setConstraints(b, 0, i);
b.setOnAction(e -&gt; {
result.append(name);
window.close();
});
layout.getChildren().add(b);
}
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.showAndWait();
return result.toString();
}

If you use a VBox, which seems more natural, you can make the code cleaner (since you don't need the list index):

public static String display(List&lt;String&gt; names) {
StringBuilder result = new StringBuilder();
Stage window = new Stage();
window.setTitle(&quot;title&quot;);
VBox layout = new VBox();
for (String name : names) {
Button b = new Button(name); 
b.setOnAction(e -&gt; {
result.append(name);
window.close();
});
layout.getChildren().add(b);
}
Scene scene = new Scene(layout, 300, 250);
window.setScene(scene);
window.showAndWait();
return result.toString();    
}

答案2

得分: 0

你基本上有两个选择。

选项 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();
    }
}
英文:

You basically have two options here.

Option 1:

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

Option 2: What I prefer

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

Full Code: I would recommend against doing this! I think you should use either Alert or Dialog to accomplish this!

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&lt;String&gt; names = new ArrayList();
names.add(&quot;john&quot;);
names.add(&quot;Kim&quot;);
Button button = new Button(&quot;List of Names Menu&quot;);
button.setOnAction((e) -&gt; {
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 &lt;String&gt; names) {
Label label = new Label();
Stage window = new Stage();
window.setTitle(&quot;title&quot;);
GridPane layout = new GridPane();
for (int i = 0; i &lt; names.size(); i++) {
Button b = new Button(names.get(i));
GridPane.setConstraints(b, 0, i);
b.setOnAction((e) -&gt; {
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();
}
}

huangapple
  • 本文由 发表于 2020年10月15日 09:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64363698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定