【问题标题】:getting the contenents of a GridPane javafx获取 GridPane javafx 的内容
【发布时间】:2014-05-09 07:11:32
【问题描述】:

我在每一行都有一个 GridPane,有一个包含标签和文本字段的 Hbox。我想获取用户在文本字段中写入的文本,同时也获取写入标签中的文本,我该怎么做?

【问题讨论】:

  • 欢迎来到 StackOverflow。当您提出问题时,请附上您寻求帮助的问题的相关代码。另外,在这里提问之前,请确保您已经对这个问题进行了一些研究,并且可以证明您已经努力自己先解决了这个问题。请参阅stackoverflow.com/questions/how-to-ask 了解更多信息。

标签: javafx textfield gridpanel


【解决方案1】:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
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.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author reegan
 */
public class GridPaneStackOverFlow extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane gp = new GridPane();
        gp.add(createNode("Stack"), 0, 0);
        gp.add(createNode("Over"),0,1);
        gp.add(createNode("Flow"), 0, 2);
        StackPane root = new StackPane();
        root.getChildren().add(gp);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

    public HBox createNode(String label) {
        HBox box = new HBox(20);
        final Label l = new Label(label);
        TextField field = new TextField();
        field.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> ov, String t, String t1) {
                System.out.println( l.getText()+ " : " +t);
            }
        });
        box.getChildren().addAll(l,field);
        return box;

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 2014-01-06
    • 2013-01-23
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多