JavaFX将自定义类添加到父节点

时间:2015-03-11 22:10:23

标签: javafx

我很感激任何帮助,我花了好几个小时都无济于事。

所以我需要将一个我在网上发现的一个名为GridDisplay的类的实例添加到Parent root中,因为我需要这个场景然后为我显示。但我还需要使用fxml文件来设置根,从而设置场景。以下是我到目前为止的情况:

@FXML private BorderPane mainPanel = new BorderPane();
@FXML private TilePane tilePane = new TilePane();
@FXML private Pane topCanvas = new Pane();
@FXML private Pane leftCanvas = new Pane();
@FXML private Pane rightCanvas = new Pane();
@FXML private Pane bottomCanvas = new Pane();
@FXML private ScrollPane scrollPane = new ScrollPane();
@FXML private Button stacksButton = new Button();
@FXML private Button queueButton = new Button();
@FXML private Button stepStacksButton = new Button();
@FXML private Button stepQueueButton = new Button();

private MazeSolver solver;
private GridDisplay gridDisplay;
private ArrayList<String> maze;
private String fileLocation;
private int height;
private int width;

@Override
public void start(Stage primaryStage) throws IOException, InterruptedException 
{
    solver = new MazeSolver(fileLocation);
    int[] dimensions = solver.getDimensions();
    this.width = dimensions[0];
    this.height = dimensions[1];
    maze = solver.getMazeLayout();

    primaryStage.setTitle("Maze Solver");


    //this is the root which uses the fxml file and in turn sets up the scene
    Parent root = FXMLLoader.load(getClass().getResource("MazeApp.fxml"));
    Scene scene = new Scene(root);


    //Represents the grid with Rectangles
    gridDisplay = new GridDisplay(height, width);
    gridDisplay.setMaze(maze);

    mainPanel = new BorderPane();

    //here i set the scroll pane as the mainPanel's content
    mainPanel.setCenter(scrollPane);

    //and then set the gridDisplay as the scroll panes content
    scrollPane.setContent(gridDisplay.getDisplay());

    //re paints the grid and its boxes
    gridDisplay.createElements();

    primaryStage.setScene(scene);
    primaryStage.show();

}

我做这些事情的顺序没有区别,它只是从未显示过。我总是留在窗口和迷宫应该在的空白区域,如下所示:https://www.dropbox.com/s/rqxa9dy2w9lw4tz/Screenshot%202015-03-11%2022.01.46.png?dl=0(抱歉链接,无法直接发布图片)。

非常感谢任何帮助。

编辑: 我的GridDisplay类:

public class GridDisplay{

private static final double ELEMENT_SIZE = 30;
private static final double GAP = ELEMENT_SIZE / 90;

private TilePane tilePane = new TilePane();
private Group display = new Group(tilePane);
private int nRows;
private int nCols;
private ArrayList<String> maze = null;

public GridDisplay(int nRows, int nCols) 
{
    tilePane.setStyle("-fx-background-color: rgba(255, 215, 0, 0.1);");
    tilePane.setHgap(GAP);
    tilePane.setVgap(GAP);
    setColumns(nCols);
    setRows(nRows);
}

public void setColumns(int newColumns) 
{
    nCols = newColumns;
    tilePane.setPrefColumns(nCols);
    createElements();
}

public void setRows(int newRows) 
{
    nRows = newRows;
    tilePane.setPrefRows(nRows);
    createElements();
}

public TilePane getDisplay() 
{
    return tilePane;
}

public void createElements() 
{
    tilePane.getChildren().clear();
    for (int i = 0; i < nRows; i++) {
        for (int j = 0; j < nCols; j++) {
            if(maze==null)
                tilePane.getChildren().add(createElement(Color.RED));
            else
            {
                Color color = Color.WHITE;
                if(Character.toString(maze.get(i).charAt(j)).equals("#"))
                    color = Color.BLACK;
                if(Character.toString(maze.get(i).charAt(j)).equals("o"))
                    color = Color.GREEN;
                if(Character.toString(maze.get(i).charAt(j)).equals("*"))
                    color = Color.RED;
                tilePane.getChildren().add(createElement(color));
            }
        }
    }
}

public void colorSolved() 
{
    tilePane.getChildren().clear();
    for (int i = 0; i < nRows; i++) {
        for (int j = 0; j < nCols; j++) {
            if(maze==null)
                tilePane.getChildren().add(createElement(Color.RED));
            else
            {
                Color color = Color.WHITE;
                if(Character.toString(maze.get(i).charAt(j)).equals("#"))
                    color = Color.BLACK;
                if(Character.toString(maze.get(i).charAt(j)).equals("o"))
                    color = Color.GREEN;
                if(Character.toString(maze.get(i).charAt(j)).equals("*"))
                    color = Color.RED;
                tilePane.getChildren().add(createElement(color));
            }
        }
    }
}

public void setMaze(ArrayList<String> maze)
{
    this.maze = maze;
}

private Rectangle createElement(Color color) {
    Rectangle rectangle = new Rectangle(ELEMENT_SIZE, ELEMENT_SIZE);
    rectangle.setStroke(Color.BLACK);
    rectangle.setFill(color);

    return rectangle;
}

}

我无法共享xml的代码,因为它不会正确粘贴到此窗口,这是一张图片:

https://www.dropbox.com/s/27tbliubdlqdfzm/Screenshot%202015-03-12%2010.09.25.png?dl=0

1 个答案:

答案 0 :(得分:0)

由于您的root是BorderPane,因此您只需在代码中设置相应的区域:

BorderPane root = FXMLLoader.load(getClass().getResource("MazeApp.fxml"));
Scene scene = new Scene(root);


//Represents the grid with Rectangles
gridDisplay = new GridDisplay(height, width);
gridDisplay.setMaze(maze);

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(gridDisplay);

root.setCenter(scrollPane);

// etc