简体   繁体   English

javafx中的图表和按钮不呈现

[英]chart and Button in javafx doesn't render

I want to render an array of buttons and then a piechart to the screen. 我想渲染一个按钮数组,然后在屏幕上绘制一个饼图。 I've tried almost every method I could but something doesn't seems to work. 我已经尝试了几乎所有可能的方法,但是某些方法似乎无效。 either alone array of buttons(usercontrol()) or pie(the graph) can be render but when I try to do both it only render the array of buttons.plz don't worry about return types of function. 按钮(usercontrol())或饼图(图形)的单独数组都可以呈现,但是当我尝试同时执行这两个操作时,它仅呈现按钮的数组。plz不必担心函数的返回类型。 any help will be really appreciated. 任何帮助将不胜感激。

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;
    }

}

Your problem is that you are creating a new Pane every time you call the pane method. 你的问题是,你正在创建一个新的Pane每次调用的时候pane的方法。 You need to change it, perhaps by using a global Pane object. 您可能需要使用全局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;
}

This should get you your desired result. 这应该给您您想要的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM