Java 返回带有onAction属性的按钮文本
我有一个方法,它接收一个名称列表,然后为列表中的每个名称创建按钮。我想以字符串的形式返回名称,或者返回列表中被单击的数字,但我发现这很难做到Java 返回带有onAction属性的按钮文本,java,javafx,Java,Javafx,我有一个方法,它接收一个名称列表,然后为列表中的每个名称创建按钮。我想以字符串的形式返回名称,或者返回列表中被单击的数字,但我发现这很难做到 public static String display(List<String> names) { Stage window = new Stage(); window.setTitle("title"); GridPane layout = new GridPane(); for (in
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;
}
但是我得到了以下错误:
局部变量是在封闭范围内定义的,必须是final或effective final
这里基本上有两个选项
选项1:
System.out.println(b.getText());
选项2:我喜欢什么
Button tempButton = ((Button)e.getSource());
System.out.println(tempButton.getText());
完整代码:我建议不要这样做!我认为您应该使用警报
或对话框
来完成此任务强>
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();
}
}
import java.util.ArrayList;
导入java.util.List;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Label;
导入javafx.scene.layout.GridPane;
导入javafx.scene.layout.StackPane;
导入javafx.stage.stage;
/**
*JavaFX应用程序
*/
公共类应用程序扩展应用程序{
@凌驾
公众假期开始(阶段){
列表名称=新的ArrayList();
姓名。添加(“约翰”);
名称。添加(“Kim”);
按钮按钮=新按钮(“名称列表菜单”);
按钮设置动作((e)->{
System.out.println(显示(名称));
});
StackPane布局=新建StackPane();
layout.getChildren().add(按钮);
场景=新场景(布局,300250);
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射();
}
公共字符串显示(列表名称){
标签=新标签();
阶段窗口=新阶段();
窗口。设置标题(“标题”);
GridPane布局=新建GridPane();
对于(int i=0;i{
label.setText(b.getText());
window.close();
});
layout.getChildren().add(b);
}
场景=新场景(布局,300250);
window.setScene(场景);
show和wait();
返回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();
}
您的按钮已经具有在
新建按钮(names.get(i).
中设置的必要信息,因此只需返回按钮标签
我猜也是如此,但我该如何做呢?返回b.getLabel()代码>?阅读javadocstryb.getText()
。类型按钮的getLabel未定义。如果我想打印标签,但我想返回标签,这是可行的。如果您看到我对问题所做的编辑,这正是我试图做的。看起来您正试图创建一个警报
或一个对话框
。我会使用内置的警报
或对话框
来执行此操作。name是一个用户列表。这个想法是,用户点击他们想要的名字,我将这个名字返回到主程序,然后继续为那个特殊的用户编写程序。这与问题几乎无关。特别是考虑到你发布的代码。无论哪种方式,想法都是一样的。我不知道为什么我没有想到詹姆斯建议的StringBuilder
。我知道你是谁,詹姆斯,但如果我遇到你,我会给你买块鸡块:)
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();
}
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();
}