簡體   English   中英

javafx中的圖表和按鈕不呈現

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

我想渲染一個按鈕數組,然后在屏幕上繪制一個餅圖。 我已經嘗試了幾乎所有可能的方法,但是某些方法似乎無效。 按鈕(usercontrol())或餅圖(圖形)的單獨數組都可以呈現,但是當我嘗試同時執行這兩個操作時,它僅呈現按鈕的數組。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;
    }

}

你的問題是,你正在創建一個新的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;
}

這應該給您您想要的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM