2017-04-25 89 views
0

说我遇到的问题是,当我试图瓷砖添加到面板边框的中央,然后是边框面板添加到根面板边框的中心,走线远离正确。JavaFX的节点对准BorderPanes

这里是我创建了一个主或“根”窗格中,我命名为mainPane。然后,我创建了bottomPanecenterPane举行,最终将被添加到mainPane底部和中心项目:

BorderPane mainPane = new BorderPane(); 

BorderPane bottomPane = new BorderPane(); 
BorderPane centerPane = new BorderPane(); 

Button quitButton = new Button("Quit Game"); 
bottomPane.setRight(quitButton); 

Text gameWinLabel = new Text(); 
gameWinLabel.setText("You win!"); 
gameWinLabel.setFont(Font.font("Times New Roman", 50)); 
gameWinLabel.setVisible(false); 
bottomPane.setCenter(gameWinLabel); 

然后我写了会产生记忆游戏瓷砖代码:

char c = 'A'; 
List<Tile> tiles = new ArrayList<>(); 
for (int i = 0; i < NUM_OF_PAIRS; i++) { 
    tiles.add(new Tile(String.valueOf(c))); 
    tiles.add(new Tile(String.valueOf(c))); 
    c++; 
} 

Collections.shuffle(tiles); 

for (int i = 0; i < tiles.size(); i++) { 
    Tile tile = tiles.get(i); 
    tile.setTranslateX(100 * (i % NUM_PER_ROW)); 
    tile.setTranslateY(100 * (i/NUM_PER_ROW)); 
    centerPane.getChildren().add(tile); 
} 

最后,我停住了容器窗格mainPane

mainPane.setBottom(bottomPane); 
mainPane.setCenter(centerPane); 

这是当前欧输入: This is the current output

最终目标是让游戏集中在mainPane的中心。

在此先感谢您的任何建议!

+1

你可能会更好使用[GridPane(https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/GridPane.html)为你的“根”这样你的只使用一个窗格并可以对齐内容。如果你想要更多的信息,我可以提出一个答案,并给你一些代码。 – TravisF

+0

@TravisF一个例子会很棒!我很难与什么窗格绑定。 – wpursell

回答

0

避免大量的奋斗,我调整的mainPane大小,使其适合游戏板的大小。我意识到这不是一个真正的答案,但为了让游戏板更好(这是我的目标),我认为我的解决方案是可以的。

感谢您的时间反正帮助!

0

正如我在我的评论中提到,一个GridPane将是您更好的选择,要做到这一点,你可以做这样的:

GridPane root = new GridPane(), centreGrid = new GridPane(); 
root.setAlignment(Pos.CENTER); 
BorderPane bottomPane = new BorderPane(); 
Integer rowCount = 0 ,colCount = 0; 

Button quitButton = new Button("Quit Game"); 
bottomPane.setRight(quitButton); 

Text gameWinLabel = new Text(); 
gameWinLabel.setText("You win!"); 
gameWinLabel.setFont(Font.font("Times New Roman", 50)); 
gameWinLabel.setVisible(false); 
bottomPane.setCenter(gameWinLabel); 

char c = 'A'; 
List<Tile> tiles = new ArrayList<>(); 
for (int i = 0; i < NUM_OF_PAIRS; i++) { 
    tiles.add(new Tile(String.valueOf(c))); 
    tiles.add(new Tile(String.valueOf(c))); 
    c++; 
} 

Collections.shuffle(tiles); 

for (int i = 0; i < tiles.size(); i++) { 
    Tile tile = tiles.get(i); 
    if (rowCount > 4) { 
     rowCount += 1; 
     colCount = 0; 
    } 
    centreGrid.add(tile, colCount, rowCount); 
    colCount += 1; 
} 

root.add(centreGrid, 0, 1); 
root.add(bottomGrid, 0, 2); 

为顶部,随意添加任何你像那里一样。有关更多信息,请参阅Oracle页面上的GridPane