英文:
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<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;
}
What I tried:
String s = "";
b.setOnAction(e - > {
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<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();
}
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<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();
}
答案2
得分: 0
你基本上有两个选择。
选项 1:
System.out.println(b.getText());
选项 2: 我更喜欢这个
Button tempButton = ((Button)e.getSource());
System.out.println(tempButton.getText());
完整代码: 我建议不要这样做!我认为你应该使用 Alert
或 Dialog
来实现这个!
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<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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论