javafx中的图表和按钮不会渲染

时间:2018-02-27 19:24:19

标签: java javafx-8

我想渲染一个按钮数组,然后在屏幕上显示一个饼图。我几乎尝试了所有可行的方法,但似乎并不起作用。或者单独的按钮数组(usercontrol())或者pie(图形)可以渲染,但是当我尝试两者时它只渲染按钮数组.plz不用担心返回类型的函数。任何帮助将非常感激。

public class Layout {

    // returns Windows height and width
    private final double width = 600;
    private final double height = 400;

    private Button[] userControl() { // navigation bar buttons
        Button[] buttons = new Button[3];

        buttons[0] = new Button("BUY Share!"); // Buy shares buttons
        buttons[0].setLayoutX(width - 100);
        buttons[0].setLayoutY(10);
        buttons[1] = new Button("Sell Shares!"); // Sell shares buttons
        buttons[1].setLayoutX(width - 200);
        buttons[1].setLayoutY(10);
        buttons[2] = new Button("Show Share"); // Show shares buttons
        buttons[2].setLayoutX(width - 300);
        buttons[2].setLayoutY(10);
        return buttons;
    }

    public void pie() {
        ObservableList<PieChart.Data> shareHolders
                = FXCollections.observableArrayList(
                        new PieChart.Data("user1", 13),
                        new PieChart.Data("user2", 25),
                        new PieChart.Data("user3", 10),
                        new PieChart.Data("user4", 22),
                        new PieChart.Data("user5", 30));
        PieChart chart = new PieChart(shareHolders);
        chart.setTitle("Share Holders Shares");
        VBox pie = new VBox();
        pie.setLayoutY(100);
        pie.getChildren().addAll(chart);
        pane().getChildren().add(pie);
        // return pie;
    }

    private Pane pane() {
        Pane pane = new Pane();

        pane.getChildren().addAll(userControl());
        return pane;
    }

    public Stage window() {

        //pane().getChildren().add();
        pie();
        Scene scene = new Scene(pane(), 600, 400);
        Stage primaryStage = new Stage();
        primaryStage.setScene(scene);
        primaryStage.setTitle("ShareHolders!");
        primaryStage.show();
        return primaryStage;
    }

}

1 个答案:

答案 0 :(得分:0)

您的问题是,每次调用Pane方法时,您都在创建新的pane。您可能需要通过使用全局Pane对象来更改它。

//First, declare a global Pane.
static Pane pane = new Pane();

//Make your pie() method return the pie VBox.
public VBox pie() {
    /*Blah blah blah, making the pie...*/
    return pie//Remember, pie is a VBox, which is why we are returning the VBox.
}

//Later, when you build your window, add the pie and the buttons to the GLOBAL PANE...
public Stage window() {
    pane.getChildren().add(pie());      //...right here.
    pane.getChildren().addAll(userControl());
    /*Build the primary stage...*/
    return primaryStage;
}

这可以为您提供理想的结果。