2014-03-29 26 views
0

我在每一行都有一個GridPane這裏有一個包含標籤和文本框的Hbox,我希望獲得用戶在文本框中寫入的文本,並且還可以獲取寫在標籤中的文本我這樣做,請幫助我謝謝。獲得GridPane的支持者javafx

+0

歡迎來到StackOverflow。當您提出問題時,請包含您要求幫助的問題的相關代碼。另外,在問這裏之前,確保你已經對這個問題做了一些研究,並且可以證明你已經努力自己先解決它。有關更多信息,請參閱http://stackoverflow.com/questions/how-to-ask。 – forgivenson

回答

0
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; 

    } 

}