返回按钮点击后的文本字段

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

Returning textfield after buttonclick

问题

我正在尝试将一个字符串从一个屏幕移动到另一个屏幕。

一旦程序启动,我会创建一个带有按钮的屏幕。

点击此按钮,我会创建一个带有文本字段和按钮的新屏幕。

我希望程序在用户点击第二个按钮后返回他们在文本字段中输入的内容。

我尝试将其放在第二个按钮的lambda内,但那行不通

( e -> {
    String name= ConfirmBox.register();
    return name;
});

第二件我注意到的是,在我的第一个按钮的actionListener中

button.setOnAction(e -> {
    String string= ConfirmBox.register();
    System.out.print(string);   
});

在我按下第一个按钮时,我得到了“null”的输出。我猜这是因为返回太快了,但是如何减慢它,以便在用户按下按钮后获得正确的返回呢?

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

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("标题");
    button = new Button("按钮");
    
    button.setOnAction(e -> {
        String string= ConfirmBox.register();
        System.out.print(string);    
    });
}
public class ConfirmBox {
    static String save; 

    public static String register() {

        Stage window = new Stage();
        
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("标题");
        
        Label label = new Label("输入文本");
        TextField textField = new TextField();
        Button button = new Button("确定");
        
        button.setOnAction(e -> 
        {
            /*
            String name= ConfirmBox.register();
            return name;
            */

            save  = textField.getText();
            window.close();
        });

        GridPane layout = new GridPane();
        GridPane.setConstraints(label, 0, 0);
        GridPane.setConstraints(textField, 0, 1);
        GridPane.setConstraints(button, 0, 2);
            
        layout.getChildren().addAll(label, textField, button);
        Scene scene = new Scene(layout, 300, 250);
        window.setScene(scene);
        window.show();

        return save;
    }   
}
英文:

I'm trying to move a string from one screen to another.

Once the program is started I create a screen with a button.

Clicking this button I create a new screen with a textField and a button.

I want the program to return what the user writes in the textfield after they click the second button.

I tried to put it inside the second buttons lambda, but that did not work

( e -> {
           	String name= ConfirmBox.register();
            return name;
        });

The second thing I noticed was that in my first buttons actionListener

button.setOnAction(e -> {
        String string= ConfirmBox.register();
        System.out.print(string);   
    });

I get the output null once i press the first button. Im guessing this is because the return is to quick, but how do I slow it down so that I get the correct return after the user have pressed the button?

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

	@Override
	public void start(Stage primaryStage) throws Exception {
		window = primaryStage;
		window.setTitle("titel");
	    button = new Button("button");
		
		button.setOnAction(e -> {
			String string= ConfirmBox.register();
	        System.out.print(string);	
		});
     }


 public class ConfirmBox {
     static String save; 

public static String register() {

	Stage window = new Stage();
	
	window.initModality(Modality.APPLICATION_MODAL);
	window.setTitle("title");
	
	Label label = new Label("enter tekst");
	TextField tekstField = new TextField();
	Button button = new Button("ok");
	
	
	
	button.setOnAction(e-> 
	{
        /*
        String name= ConfirmBox.register();
        return name;
        */

	save  = tekstField.getText();
	window.close();
	});

	GridPane layout = new GridPane();
	GridPane.setConstraints(label, 0, 0);
	GridPane.setConstraints(tekstField, 0, 1);
	GridPane.setConstraints(button, 0, 2);
		
	layout.getChildren().addAll(label, tekstField, button);
	Scene scene = new Scene (layout, 300, 250);
	window.setScene(scene);
	window.show();

	return save;
	  	
	}	
}

答案1

得分: 1

你说得对,return语句在实质上会立即执行,因此在用户按下按钮之前就会设置String save

取而代之地,使用 window.showAndWait(),而不是window.show(),这将阻塞执行,直到窗口关闭,从而实现所需的结果。请注意,在此时没有真正的理由为此设置变量,您可以直接查找文本字段中的值:

public class ConfirmBox {

    public static String register() {

        Stage window = new Stage();

        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("title");

        Label label = new Label("enter tekst");
        TextField tekstField = new TextField();
        Button button = new Button("ok");

        button.setOnAction(e -> window.close());

        GridPane layout = new GridPane();
        GridPane.setConstraints(label, 0, 0);
        GridPane.setConstraints(tekstField, 0, 1);
        GridPane.setConstraints(button, 0, 2);

        layout.getChildren().addAll(label, tekstField, button);
        Scene scene = new Scene(layout, 300, 250);
        window.setScene(scene);

        //window.show();

        window.showAndWait();

        return tekstField.getText();

    }
}

值得注意的是,你在某种程度上重新发明了轮子。可以查看 TextInputDialog,以及类似 DialogAlert 的相关类。

英文:

You are correct that the return statement is executed essentially immediately, so before the user has pressed the button, causing the String save to be set.

Instead of window.show(), use window.showAndWait(), which will block execution until the window is closed, achieving the desired result. Note that there's no real reason to have a variable for this at this point, you can just look up the value in the text field:

public class ConfirmBox {

    public static String register() {
    
        Stage window = new Stage();
        
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("title");
        
        Label label = new Label("enter tekst");
        TextField tekstField = new TextField();
        Button button = new Button("ok");
        
        
        
        button.setOnAction(e -> window.close());
    
        GridPane layout = new GridPane();
        GridPane.setConstraints(label, 0, 0);
        GridPane.setConstraints(tekstField, 0, 1);
        GridPane.setConstraints(button, 0, 2);
            
        layout.getChildren().addAll(label, tekstField, button);
        Scene scene = new Scene (layout, 300, 250);
        window.setScene(scene);
        
        //window.show();
 
        window.showAndWait();
    
        return tekstField.getText();
        
    }   
}

It's worth remarking here that you're reinventing the wheel to some degree. Have a look at the TextInputDialog class, and related classes such as Dialog and Alert.

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

发表评论

匿名网友

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

确定