如何将菜单栏和图表放在同一场景中

时间:2016-03-14 12:01:01

标签: javafx javafx-8

嘿伙计们我是学生,我刚开始javafx,我需要帮助。 我已经创建了一个类,允许我生成菜单栏和图表,他们都以独立的方式工作,但我的问题是如何在每个图表栏类添加菜单栏,所以我可以在图表之间轻松导航我真的一直在寻找它,今晚大部分都要完成

这是源代码

    package mto.cr.GUI;
     import java.util.logging.Level;
     import java.util.logging.Logger;
     import javafx.application.Application;
     import static javafx.application.Application.launch;
     import javafx.event.ActionEvent;
     import javafx.event.EventHandler;
     import javafx.scene.Scene;
     import javafx.scene.control.*;
     import javafx.scene.layout.BorderPane;
     import javafx.scene.layout.StackPane;
     import javafx.scene.layout.VBox;
     import javafx.stage.Stage;

   public class MenuMain extends Application {

Stage window;
BorderPane layout;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage)  {
    window = primaryStage;
    window.setTitle("Crowd Rise");

    //statistique menu
    Menu statisqueMenu = new Menu("Statistique ");

    //Attribution Experience declaration setonaction et add dans le menu
    MenuItem attributExperience = new MenuItem("Attribution Experience");
    attributExperience.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            Statistique1 st1=new Statistique1() ;

          st1.start(window);




        }
    });
    statisqueMenu.getItems().add(attributExperience);
    //financement projet
     MenuItem FinancementProjet = new MenuItem("Financement Projet");
    FinancementProjet.setOnAction( new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

        }
    }
    );
    statisqueMenu.getItems().add(FinancementProjet);
    //projet
     MenuItem projet = new MenuItem("projet");
    projet.setOnAction( new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

        }
    });
    statisqueMenu.getItems().add(projet);
    //separeteur avec un trait
    statisqueMenu.getItems().add(new SeparatorMenuItem());
    //user
    MenuItem user = new MenuItem("User");
    user.setOnAction( new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

        }
    });
    statisqueMenu.getItems().add(user);


    statisqueMenu.getItems().add(new SeparatorMenuItem());
    statisqueMenu.getItems().add(new MenuItem("Exit..."));

    //recherche menu
    Menu rechercheMenu = new Menu("Recherche");
    //user
     MenuItem userR = new MenuItem("User");
    userR.setOnAction( new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

        }
    });
    rechercheMenu.getItems().add(userR);
    //projet
     MenuItem projetR = new MenuItem("Projet");
    projetR.setOnAction( new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

        }
    });
    rechercheMenu.getItems().add(projetR);        
    //probleme
    MenuItem problemeR = new MenuItem("Probleme recherche");
    problemeR.setOnAction( new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

        }
    });
    rechercheMenu.getItems().add(problemeR);






    //Main menu bar
    MenuBar menuBar = new MenuBar();
    menuBar.getMenus().addAll(statisqueMenu,rechercheMenu);

    layout = new BorderPane();
    layout.setTop(menuBar);

    Scene scene = new Scene(layout, 400, 300);
    window.setScene(scene);
    window.show();
}

}

我还有另一个允许我创建图表的类,这是源代码

    package mto.cr.GUI;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.chart.LineChart;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;


    public class Statistique1 extends Application {

@Override public void start(Stage stage) {
    MenuMain menumain=  new MenuMain();
    menumain.start(stage);
    stage.setTitle("Line Chart Sample");
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    //creating the chart
    final LineChart<Number,Number> lineChart = 
            new LineChart<Number,Number>(xAxis,yAxis);

    lineChart.setTitle("Stock Monitoring, 2010");
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("My portfolio");
    //populating the series with data
    series.getData().add(new XYChart.Data(1, 23));
    series.getData().add(new XYChart.Data(2, 14));
    series.getData().add(new XYChart.Data(3, 15));
    series.getData().add(new XYChart.Data(4, 24));
    series.getData().add(new XYChart.Data(5, 34));
    series.getData().add(new XYChart.Data(6, 36));
    series.getData().add(new XYChart.Data(7, 22));
    series.getData().add(new XYChart.Data(8, 45));
    series.getData().add(new XYChart.Data(9, 43));
    series.getData().add(new XYChart.Data(10, 17));
    series.getData().add(new XYChart.Data(11, 29));
    series.getData().add(new XYChart.Data(12, 25));
    Scene scene  = new Scene(lineChart,800,600);
    lineChart.getData().add(series);

    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    launch(args);
}
}

1 个答案:

答案 0 :(得分:1)

您无法启动多个Application。这就是为什么你应该将你想要独立使用的任何代码从Application分离到不扩展Application的类。此外,您还有重复的代码,这使得维护代码变得更加困难(请参阅DRY)。

由于您的意图似乎是替换BoderPane的中心节点,因此我建议为要在中心显示的内容而不是Application子类创建工厂类,并创建用于创建的函数菜单项:

MenuItem createMenuItem(String text, final Supplier<Node> factory) {
    MenuItem item = new MenuItem(text);
    item.setOnAction(evt -> {
        setContent(factory);
    });
}

void setContent(Supplier<Node> factory) {
    Node content = factory.get();
    layout.setCenter(content);
}

@Override
public void start(Stage primaryStage)  {
    window = primaryStage;
    window.setTitle("Crowd Rise");

    //statistique menu
    Menu statisqueMenu = new Menu("Statistique", null, 
                                      createMenuItem("Attribution Experience", new Statistique1()),
                                      createMenuItem("Financement Projet", new Statistique2()),
                                      ...);

    ...
 public class Statistique1 implements Supplier<Node> {

    @Override
    public Node get() {
        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");
        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        //populating the series with data
        series.getData().add(new XYChart.Data(1, 23));
        series.getData().add(new XYChart.Data(2, 14));
        series.getData().add(new XYChart.Data(3, 15));
        series.getData().add(new XYChart.Data(4, 24));
        series.getData().add(new XYChart.Data(5, 34));
        series.getData().add(new XYChart.Data(6, 36));
        series.getData().add(new XYChart.Data(7, 22));
        series.getData().add(new XYChart.Data(8, 45));
        series.getData().add(new XYChart.Data(9, 43));
        series.getData().add(new XYChart.Data(10, 17));
        series.getData().add(new XYChart.Data(11, 29));
        series.getData().add(new XYChart.Data(12, 25));
        lineChart.getData().add(series);

        return lineChart;
    }

}