无法将文本字段转换为字符串。

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

Cannot convert textfield into string

问题

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class GreeterApplication extends Application {

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

    @Override
    public void start(Stage window) throws Exception {

        //1. 创建视图
        //1.1 创建要使用的组件
        Label intro = new Label("输入你的名字然后开始。");
        Button start = new Button("开始");
        TextField input = new TextField();

        //1.2 创建新布局
        GridPane layout = new GridPane();
        layout.add(intro, 0, 0);
        layout.add(input, 0, 1);
        layout.add(start, 0, 2);

        // 1.3 设置布局样式
        //1.4 创建视图本身并将其设置为使用布局
        Scene first = new Scene(layout);

        //2. 创建新视图
        StackPane welcome = new StackPane();
        String name = input.getText();
        Label welcomeText = new Label("欢迎 " + input + "!"); //inpu
        welcome.getChildren().add(welcomeText);
        Scene welcomeView = new Scene(welcome);

        //3. 添加事件处理程序
        start.setOnAction((event) -> {
            if (!input.getText().isEmpty()) {
                window.setScene(welcomeView);
            }
        });

        window.setScene(first);
        window.show();
    }
}
英文:

Hi I'm currenntly doing a coding exercise where I create an application with 2 views. Create in it an application with two views. The first view should have a text field that's used to ask for the user's name. The second view then shows the user a greeting text. The greeting should be of the form "Welcome name!" where the user's name is inserted in place of 'name'.


package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GreeterApplication extends Application {
public static void main(String[] args) {
launch(GreeterApplication.class);
}
@Override
public void start(Stage window) throws Exception {
//1. Creating the view
//1.1 Creating components to be used
Label intro = new Label("Enter your name and start.");
Button start = new Button("start");
TextField input = new TextField();
//1.2 creating new layout
GridPane layout = new GridPane();
layout.add(intro, 0, 0);
layout.add(input, 0, 1);
layout.add(start, 0, 2);
// 1.3 Styling the layout
//1.4 creating view itself and setting it to use the layout
Scene first = new Scene(layout);
//2. Creating new view
StackPane welcome = new StackPane();
String name = input.getText();
Label welcomeText = new Label("Welcome " + input + "!"); //inpu
welcome.getChildren().add(welcomeText);
Scene welcomeView = new Scene(welcome);
//3. Adding event handler
start.setOnAction((event) -> {
if (!input.getText().isEmpty()) {
window.setScene(welcomeView);
}
});
window.setScene(first);
window.show();
}
}

I tried converting input into a string via input.getText() and input.toString() but I have had no success.

答案1

得分: 1

请注意以下编辑。
Label welcomeText = new Label();
标签的文本应在接收到用户输入后进行设置。
因此,您可以创建一个名为 welcomeText 的标签,并在事件处理程序中更新其文本值,使用 welcomeText.setText(input.getText())

英文:

Kindly note the following edit.
Label welcomeText = new Label();
The text of the label should only be set after receiving the input from the user.
Hence, you can create a welcomeText Label and update its text value in the event handler using welcomeText.setText(input.getText()).

huangapple
  • 本文由 发表于 2020年7月24日 08:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064911.html
匿名

发表评论

匿名网友

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

确定