ก็จะประสบความสำเร็จในส่วยูนิคอลัมน์ความกว้างสำหรับแต่ละออยู่คนละแถวด้ว GridPane?

0

คำถาม

ฉันกำลังพยายามรุ่นของบัตรเครดิตการ์ดข้อมูลอยู่ JavaFx ใช้ GridPane:

ของฉันนางแบบบรรจุ 3 แถว(ข้อควรจำ:กันและช่องข้อมูลรวมกันด้วของป้ายชื่อ+ข้อความ):

แถว 1:แรกชื่อและเมื่อชื่อของ(4 ช่องข้อมูล)

แถว 2:เลขบัตรเครดิต(2 ช่องข้อมูล)

แถว 3:วันหมดอายุองเดือน,ปี+CVV(6 ช่องข้อมูล)

เห็นภาพหน้าจอที่จับได้ด้านล่างนี้:

Card Data Model

ฉันกำลังอ่านหนังสือท่านอ่าน นี้หัดเล่น ซึ่งรัฐ:

ทุกเซลล์ที่อยู่ในที่เดียวกันแถวจะมีคนเดียวกับความสูงและทุกเซลล์ที่อยู่ใน เหมือนคอลัมน์จะต้องได้คนเดียวกับความกว้าง. ต่างออกแถวได้ แตกต่างไฮและแตกต่างคอลัมน์จะต้องแตกต่าง widths.

มีการวนรอบการทำงานที่จะต้องแตกต่างขนาดสดมภ์อยู่แถวโดยแถวเลขฐานใน GridPane?

java javafx user-interface
2021-11-23 23:01:09
2

คำตอบที่ดีที่สุด

7

สำหรับเจาะจงผังแป้นพิมพ์ในรูปภาพฉันคงต้องใช้ VBox กับ HBox สำหรับนวนแถว:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

public class App extends Application {

    @Override
    public void start(Stage stage) {

        Label lblFirst = new Label("First");
        Label lblLast = new Label("Last");
        Label lblNumber = new Label("Card Number");
        Label lblMonth = new Label("Month");
        Label lblYear = new Label("Year");
        Label lblCVV = new Label("CVV");
        
        TextField txtFirst = new TextField();
        TextField txtLast = new TextField();
        TextField txtNumber = new TextField();
        TextField txtMonth = new TextField();
        TextField txtYear = new TextField();
        TextField txtCVV = new TextField();

        HBox row1 = new HBox(10);
        HBox row2 = new HBox(10);
        HBox row3 = new HBox(10);
        
        row1.getChildren().add(createCell(lblFirst, txtFirst));
        row1.getChildren().add(createCell(lblLast, txtLast));
        
        row2.getChildren().add(createCell(lblNumber, txtNumber));
        
        row3.getChildren().add(createCell(lblMonth, txtMonth));
        row3.getChildren().add(createCell(lblYear, txtYear));
        row3.getChildren().add(createCell(lblCVV, txtCVV));
        
        VBox rows = new VBox(10, row1, row2, row3);
        
        StackPane.setMargin(rows, new Insets(10));
        
        StackPane root = new StackPane(rows);

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    private static HBox createCell(Label label, TextField text) {
        HBox pane = new HBox(5, label, text);
        label.setMinWidth(Pane.USE_PREF_SIZE);
        text.setMinWidth(50);
        pane.setAlignment(Pos.CENTER);
        HBox.setHgrow(pane, Priority.ALWAYS);
        HBox.setHgrow(text, Priority.ALWAYS);
        return pane;
    }

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

}

แสดงผล:

2021-11-24 00:54:15

ไม่อยากรหัสปรับขนาดคำใบ้ไว้
kleopatra
5

+1 สำหรับสิ่งที่@JamesD แนะนำและ@ปี่โอโบคำตอบ เข้าท่าดีนิเราจะทำทุกอย่างที่เราทำฉันจะแยกแต่ละแถวด้วผังแป้นพิมพ์เพื่อรับมือกับมันในทางที่ง่ายกว่าทำให้มันซับซ้อนใช้คนเดียวที่ GridPane.

ถึงจะพูดอย่างนั้นถ้าคุณอยากไปกับหรือเรียนรู้เกี่ยวกับเรื่องที่เธอสามารถทำอย่างที่คล้ายกัน layouting โดยใช้หนึ่ง GridPane ที่ด้านล่าง implemenation นอาจจะช่วยให้คุณเร็วความคิด

แยกกันอย่างแรกเลยก็คือผังแป้นพิมพ์ของคุณเข้าไปในการคอลัมน์ที่จะคิดให้ออกไปกี่คนทั้งหมดสดมภ์คุณต้องการ (เป็นที่ด้านล่างรูปภาพ)

enter image description here

ตอนนี้คุณจะรู้ว่าจุดเชื่อมต่ออยู่ข้างในซึ่งคอลัมน์และอีกกี่คอลัมน์มันจะดำรง(colspan)

ฉันจะอธิบายสำหรับหนึ่งโหนด:

ปล่อยให้บอกเอง..ว่าคุณต้องการแทรกสนามของชื่อต้นนาง ถ้าคุณสังเกตเห็นในรูปมันอยู่ใน rowIndex:0,columnIndex:1 และมันเป็นมืออาชีพ 4 คอลัมน์ที่ colSpan ค่าจะเป็น 4. ที่นี่เราไม่ใช่การรวมพลังที่กระจัดการอะไรแถวดังนั้น rowSpan ค่าจะถูกเสมอ 1 เลย

pane.add(getField(), 1, 0, 4, 1); // node, colIndex, rowIndex, colSpan, rowSpan

เหมือนๆกันคุณสามารถเชื่อมโยงที่เหลือของโหนดต่างๆ layouting. แล้วก็สำหรับมากก precising คุณสามารถตั้งค่า prefered ความกว้างของแต่ละคอลัมน์โดยใช้ ColumnConstraints. ด้านล่างนี้เป็นที่สมบูรณ์แบรหัสสำหรับผังแป้นพิมพ์&โครงสราง:

enter image description here

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;
    }
}
2021-11-24 00:47:44

ไม่อยากรหัสปรับขนาดคำใบ้ไว้
kleopatra

@genericname,ขอบคุณสำหรับคำแนะนำ ถ้าผมเข้าใจอย่างถูกต้อง,คุณคือการพูดถึงเรื่องการใช้พื้นทีี่ของ USE_PREF_SIZE&MAX_VALUE. ใช่ไหม? ถ้าใช่คุณสามารถได้โปรดแนะนำของตัวเลือกสำหรับใช้พวกเขาอย่างที่ฉั couldnt หาทางที่จะยืนยันป้ายชื่อของมินความกว้างที่จะไม่แสดง"..."และข้อความกล่องโดยอัตโนมัติยืดแข้งยืดที่มีอวกาศ ถ้าไม่มีคุณสามารถอธิบายหน่อยเรื่องคำใบ้ไว้คุณกำลังพูดถึง
Sai Dandem

ไม่ฉันหมายถึงที่อยากอาการหนักจริงของตัวเลขผ่านไป getCc -ที่ sentinels กำหนดไว้ในเขตพื้นที่คือโอเคเหมือนกั bounderies ของจำนวนแถบแสงแยกสี:)
kleopatra

แต่นี่คือผังแป้นพิมพ์ pathologic จากรายการออกแบบมุมมอง-มันเป็นการบุกรุกกแต่ละคนและทุกคนกฏที่ต้องการออกแบบที่ดีของผู้ใช้ส่วนติดต่อ. ดังนั้นหวังว่ามันเป็นแค่แบบฝึกหัดที่จะเรียนรู้วิธีการใช้และ limitations ของ gridpane;)
kleopatra

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่