2016-02-11 12 views
5

जावा स्विंग में GridLayout है, जो आपको 3X4 जैसे विजेटों की एक सरणी के लिए आकार निर्दिष्ट करने की अनुमति देता है। विजेट तब पैनल को भरते हैं जिसे वे कब्जा करते हैं। JavaFX में आप एक समान प्रभाव कैसे प्राप्त करते हैं?मैं javafx ग्रिडपेन भरने के लिए बटन कैसे प्राप्त करूं?

उत्तर

15

मुझे लगता है कि आप पूछ रहे हैं कि आप एक ग्रिड फलक में अपने सेल को आवंटित स्थान को भरने के लिए नोड कैसे प्राप्त करते हैं।

ऐसा करने के कुछ तरीके हैं। आप या तो नोड्स GridPane विशिष्ट गुण सेटिंग लागू करने के लिए स्थिर GridPane तरीकों का उपयोग कर सकते हैं:

GridPane grid = new GridPane(); 
for (int rowIndex = 0; rowIndex < numRows; rowIndex++) { 
    RowConstraints rc = new RowConstraints(); 
    rc.setVgrow(Priority.ALWAYS) ; // allow row to grow 
    rc.setFillHeight(true); // ask nodes to fill height for row 
    // other settings as needed... 
    grid.getRowConstraints().add(rc); 
} 
for (int colIndex = 0; colIndex < numColumns; colIndex++) { 
    ColumnConstraints cc = new ColumnConstraints(); 
    cc.setHgrow(Priority.ALWAYS) ; // allow column to grow 
    cc.setFillWidth(true); // ask nodes to fill space for column 
    // other settings as needed... 
    grid.getColumnConstraints().add(cc); 
} 

:

GridPane.setFillWidth(myButton, true); 
GridPane.setFillHeight(myButton, true); 

दूसरी तरह ग्रिड फलक पर ही स्तंभ की कमी और लगातार कमी निर्दिष्ट करने के लिए है किसी भी मामले में, आपको ग्रिड फलक बनाने के अनुरोध को सम्मानित करने के लिए नोड्स को बढ़ने की आवश्यकता होती है। तो, उदा .:

Button myButton = new Button("Click"); 
myButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
grid.add(myButton, 0, 0); 

SSCCE (पंक्ति और स्तंभ की कमी विधि का उपयोग कर):

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.ColumnConstraints; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.RowConstraints; 
import javafx.stage.Stage; 

public class KeyPad extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     GridPane grid = new GridPane(); 
     int numRows = 4 ; 
     int numColumns = 3 ; 
     for (int row = 0 ; row < numRows ; row++){ 
      RowConstraints rc = new RowConstraints(); 
      rc.setFillHeight(true); 
      rc.setVgrow(Priority.ALWAYS); 
      grid.getRowConstraints().add(rc); 
     } 
     for (int col = 0 ; col < numColumns; col++) { 
      ColumnConstraints cc = new ColumnConstraints(); 
      cc.setFillWidth(true); 
      cc.setHgrow(Priority.ALWAYS); 
      grid.getColumnConstraints().add(cc); 
     } 

     for (int i = 0 ; i < 9 ; i++) { 
      Button button = createButton(Integer.toString(i+1)); 
      grid.add(button, i % 3, i/3); 
     } 
     grid.add(createButton("#"), 0, 3); 
     grid.add(createButton("0"), 1, 3); 
     grid.add(createButton("*"), 2, 3); 

     Scene scene = new Scene(grid); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private Button createButton(String text) { 
     Button button = new Button(text); 
     button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
     button.setOnAction(e -> System.out.println(text)); 
     return button ; 
    } 

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

बढ़िया! धन्यवाद! +1 और स्वीकार करें। – ncmathsadist

+1

इसे सीएसएस में करने के लिए: (मेरे मामले में, मैक्सविड्थ इन्फिनिटी सेट करना पर्याप्त था) '.mygridPane बटन { \t -fx-max-width: अनंतता; } ' – pdem

-3

आप इसके लिए जावाएफएक्स में ग्रिडपेन का उपयोग कर सकते हैं। जावा कोड में, आप इसे से

GridPane pane = new GridPane(); 

बना सकते हैं और तरह

Label label = new Label(); 
pane.add(label, 0,0); 

यह करने के लिए अलग-अलग नोड्स जोड़ने के लिए, उदाहरण के लिए कर सकते हैं यह पहली पंक्ति और GridPane के पहले स्तंभ के लिए एक लेबल जोड़ देगा । अधिक जटिल जीयूआई के लिए, आप सीनबुल्डर का उपयोग कर सकते हैं।

+1

मैं एक ग्रिड फलक पर चीजों को जोड़ने के बारे में पता है। जो कुछ मैं चाहता हूं वह है कि, ग्रिडपेन पर कब्जा कर सकने वाले क्षेत्र को पूरी तरह से भरने के लिए बटनों की एक सरणी कहें। – ncmathsadist

संबंधित मुद्दे