2017-12-18 13 views
0

グリッドパネルレイアウトを使用して別のシーンを作成したいとします。基本的にはすべてのコードをコピーして新しいシーンを作成しようとしましたが、2番目のシーンに配置したものはすべてグリッドにスナップしません。私はかなり新しいコーディング、これは私の5番目のプロジェクトです。 (悪いコードで申し訳ありません)2つの異なるシーンで1つのクラスで2つのGridPaneを取得

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.layout.GridPane; 
import javafx.stage.Stage; 

public class Main extends Application { 

    Stage window; 
    Scene s1, s2; 

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

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     window = primaryStage; 
     window.setTitle("Test"); 

     GridPane grid = new GridPane(); 
     grid.setPadding(new Insets(10, 10, 10, 10)); 
     grid.setVgap(10); 
     grid.setHgap(10); 

     Button b1 = new Button("Scene 2"); 
     b1.setOnAction(e -> window.setScene(s2)); 
     GridPane.setConstraints(b1, 2, 0); 

     Button b2 = new Button("Button 2"); 
     GridPane.setConstraints(b2, 5, 0); 

     grid.getChildren().addAll(b1, b2); 
     Scene s1 = new Scene(grid, 500, 500); 

     GridPane grid2 = new GridPane(); 
     grid.setPadding(new Insets(10, 10, 10, 10)); 
     grid.setVgap(10); 
     grid.setHgap(10); 

     Button b3 = new Button("Scene 1"); 
     b3.setOnAction(e -> window.setScene(s1)); 
     GridPane.setConstraints(b3, 0, 0); 

     Button b4 = new Button("Button 2"); 
     GridPane.setConstraints(b4, 6, 0); 

     grid2.getChildren().addAll(b3, b4); 
     s2 = new Scene(grid2, 500, 500); 


     window.setScene(s1); 
     window.show(); 
    } 


} 
+1

でなければなりません。 –

+1

「グリッドにスナップしない」とはどういう意味ですか?私はあなたのコードを実行し、シーン2では適切な間隔が表示されないことを除いて、うまく動作します。これはJamesが言ったように、grid2のvgapとhgapを設定していないからです。 –

+0

あまりにも@ James_Dありがとうございました。私はgrid.setPaddingなどがgrid2とラベル付けされていなければならないことに気付きませんでした。私はそれが一般的だと思った。 – CraftMaster100

答えて

2

コードにコピー&ペーストエラーがあります。

GridPane grid2 = new GridPane(); 
    grid.setPadding(new Insets(10, 10, 10, 10)); 
    grid.setVgap(10); 
    grid.setHgap(10); 

おそらく、最初のグリッドペインにパディングとギャップが適用された上記のコードをコピーしたものです。あなたはgrid2` `に詰め、hgap、またはVGAPを設定しませんでした:第2のグリッドペインにそれらを適用するには、それはあなただけの明白なコピー&ペーストのエラーを持っている

GridPane grid2 = new GridPane(); 
    grid2.setPadding(new Insets(10, 10, 10, 10)); 
    grid2.setVgap(10); 
    grid2.setHgap(10); 
関連する問題