2016-04-12 7 views
0

私は、5行2列の1-10とラベルされたボタンを備えたエレベータボタンパッドシミュレータを作成しようとしています。ここで私が説明しようとしているものの例である:JavaFX:2列のボタンを作成する方法は?

9  10 
7   8 
5   6 
3   4 
1   2 

現在、私のコードは、10個のボタンがありますが、すべては、私はしたくない一つの列にマージ。私のコードのちょうどスニペットがあり、ここで

1 
1 
2 
2 
3 
3 
4 
4 
5 
5 

をと:(コメント部分を気にしない)

@Override 
public void start(Stage primaryStage) { 
    // Get the pane for the scene 
    primaryStage.setScene(new Scene(getPane(), 180, 600)); 

    // Setup the stage 
    primaryStage.setTitle("Elevator Buttons"); 
    primaryStage.setResizable(false); 
    primaryStage.show(); 
} 

protected Pane getPane() { 
    Pane pane = new VBox(10); 
    pane.setPadding(new Insets(40)); 
    GridPane grid = new GridPane(); 
    //for (int i = row - 1; i >= 0; i-=2) { 
     // for (int k = col - 1; k >= 0; i-=1) { 

    for (int i = 0; i < row; i++) { 
     for(int k =0; k <col; k++) { 
      // Set the button number as text for the button 
      buttonsArray[i][k] = new Button(Integer.toString(i + 1)); 

      // Set preferred width and style with a light gray background 
      buttonsArray[i][k].setPrefWidth(100); 
      buttonsArray[i][k].setStyle("-fx-font: 22 arial; -fx-base: LightGray"); 

      // Add the button to the pane and set the handler 
      pane.getChildren().add(buttonsArray[i][k]); 
      buttonsArray[i][k].setOnAction(ButtonHandler); 
     } 
    } 
    return pane; 
} 

は、どのように私は、ボタンの2つの列を作るだろうとこのようになりますアレイ?

+3

[ 'GridPane'](http://docs.oracle.com/javase/8/javafx/api/javafx/を使用しますscene/layout/GridPane.html)で、ボタンを保持し、 'getPane()'メソッドからそれを返すように宣言します。 'VBox'を完全に取り除く。 –

答えて

2

コメントやその他の回答で指摘されているように、達成するためにGridPaneを使用することができます。このコードは、エレベータのボタンの正しいラベル付けを含め、仕事をする必要があります。

@Override 
    public void start(Stage primaryStage) { 
     // Get the pane for the scene 
     primaryStage.setScene(new Scene(getPane(), 180, 600)); 

     // Setup the stage 
     primaryStage.setTitle("Elevator Buttons"); 
     primaryStage.setResizable(false); 
     primaryStage.show(); 
    } 

    protected Pane getPane() { 
     Pane pane = new VBox(10); 
     pane.setPadding(new Insets(40)); 
     GridPane grid = new GridPane(); 

     for (int i = 0; i < row; i++) { 
      for (int k = 0; k < col; k++) { 
       // Set the button number as text for the button 
       Button button = new Button(Integer.toString((row * col - 2 * i - 1) + k)); 

       button.setPrefWidth(100); 
       button.setStyle("-fx-font: 22 arial; -fx-base: LightGray"); 

       // Add the button to the pane and set the handler 
       grid.add(button, k, i); 
       button.setOnAction(buttonHandler); 
      } 
     } 
     return grid; 
    } 
0

交換この:これで

pane.getChildren().add(buttonsArray[i][k]); 

grid.add(buttonsArray[i][k],k,i); 

James_Dは彼のコメントに提案しているものです。

+0

それはかなりうまくいきませんでした。これは、トリックを行うように見えた:grid.add(buttonsArray [i] [k]、k、i);ありがとう!! –

+0

それは私が望んだものです:P私は答えを変更:) – DVarga

関連する問題