+1 के लिए क्या @JamesD सुझाव और @ओबाउ जवाब. आदर्श रूप में मैं अलग-अलग प्रत्येक पंक्ति लेआउट, इसे संभाल करने के लिए आसान तरीके से बनाने की तुलना में यह जटिल का उपयोग केवल एक GridPane.
वाले ने कहा कि, अगर आप चाहते हैं के साथ जाने के लिए या सीखने के बारे में कैसे आप कर सकते हैं इसी तरह की layouting का उपयोग कर एक GridPane, नीचे implemenation दे सकता है आप एक त्वरित विचार है ।
सबसे पहले विभाजन अपने लेआउट में आवश्यक कॉलम, के लिए बाहर आंकड़ा कुल कितने कॉलम आप की जरूरत है. (के रूप में छवि के नीचे)
अब आप पता चल जाएगा कौन सा नोड में बैठ जाएगा, जो स्तंभ और कितने कॉलम यह कब्जा होगा (colspan)
मैं समझाने के लिए एक नोड:
चलो कहते हैं कि आप चाहते हैं फ़ील्ड सम्मिलित पहला नाम का. अगर तुम नोटिस चित्र में, यह rowIndex: 0, columnIndex: 1 और के कब्जे में है 4 कॉलम, तो colSpan मान 4 हो जाएगा. यहाँ हम नहीं कर रहे हैं के संयोजन के किसी भी पंक्तियों, तो रोस्पेन मान हमेशा 1 होगा.
pane.add(getField(), 1, 0, 4, 1); // node, colIndex, rowIndex, colSpan, rowSpan
इसी तरह से आप संबंधित कर सकते हैं के बाकी नोड्स layouting. और भी अधिक के लिए precising आप सेट कर सकते हैं पसंदीदा प्रत्येक स्तंभ की चौड़ाई का उपयोग कर ColumnConstraints. नीचे के लिए पूरा कोड के लेआउट और बाधाओं:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CreditCardPaneDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setPadding(new Insets(5));
root.setSpacing(10);
Scene scene = new Scene(root,300,200);
stage.setScene(scene);
stage.setTitle("CreditCard");
stage.show();
GridPane pane = new GridPane();
pane.setStyle("-fx-border-color:black;-fx-border-width:1px;-fx-background-color:yellow");
pane.setPadding(new Insets(5));
pane.setHgap(5);
pane.setVgap(5);
pane.add(getLabel("First"), 0, 0, 1, 1);
pane.add(getField(), 1, 0, 4, 1);
pane.add(getLabel("Last"), 5, 0, 1, 1);
pane.add(getField(), 6, 0, 2, 1);
pane.add(getLabel("Card Number"), 0, 1, 3, 1);
pane.add(getField(), 3, 1, 5, 1);
pane.add(getLabel("Month"), 0, 2, 2, 1);
pane.add(getField(), 2, 2, 2, 1);
pane.add(getLabel("Year"), 4, 2, 1, 1);
pane.add(getField(), 5, 2, 1, 1);
pane.add(getLabel("CVV"), 6, 2, 1, 1);
pane.add(getField(), 7, 2, 1, 1);
pane.getColumnConstraints().addAll(getCc(70), getCc(20), getCc(80), getCc(20), getCc(25), getCc(90), getCc(80), getCc(100));
CheckBox gridLines = new CheckBox("Show grid lines");
gridLines.selectedProperty().addListener((obs, old, val) -> pane.gridLinesVisibleProperty().set(val));
root.getChildren().addAll(gridLines, pane);
}
private ColumnConstraints getCc(double width) {
ColumnConstraints cc = new ColumnConstraints();
cc.setPrefWidth(width);
return cc;
}
private Label getLabel(String txt) {
Label lbl = new Label(txt);
lbl.setMinWidth(Region.USE_PREF_SIZE);
return lbl;
}
private TextField getField() {
TextField field = new TextField();
field.setMaxWidth(Double.MAX_VALUE);
return field;
}
}