mercredi 29 avril 2015

Having trouble resetting an equation in javafx

I've searched here for this answer and either I'm not using the proper words in my searches, or the question hasn't been asked.

I have a program that gives the user a randomly selected equation. The user works through 3 scenes to solve the equation, then is asked if they want to solve another. If they choose yes, I want the program to return to scene 1, and ask them to click "Load" for another problem. When they do this, the program would randomly select another equation and display it in Scene 2.

The problem I'm having is that when the user chooses yes, the program returns them to scene 1, asks them to click "Load," then takes them to the END of scene 2, displaying the end result of the equation they have already worked on.

I need help with clearing those values and displaying a new equation for the user.

I'm using Eclipse, and I've cut out A LOT of code just to give the relevant parts of the program that are giving me trouble. Thank you in advance for your help.

public class equationsapp extends Application implements EventHandler<ActionEvent> {

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

    @Override public void start(Stage primaryStage) {

        stage = primaryStage;

        Text welcome = new Text("Click the button to load an equation");

        Button begin = new Button("Load");
        begin.setOnAction(e -> begin_Click());

        HBox letsbegin = new HBox();
        letsbegin.getChildren().addAll(welcome);

        HBox letsbegin2 = new HBox();
        letsbegin2.getChildren().addAll(begin);

        GridPane startitoffgp = new GridPane();
        startitoffgp.add(letsbegin, 0, 0);
        startitoffgp.add(letsbegin2, 0, 1);

        startitoff.setCenter(startitoffgp);

        //Add BorderPane to Scene
        scene1 = new Scene(startitoff, 500, 300);       

        //Add the scene to the stage, set the title and show the stage
        primaryStage.setScene(scene1);
        primaryStage.setTitle("Equations");
        primaryStage.show();

        setreset();
    }

    public void begin_Click() {
        stage.setScene(scene2);
    }

    public void setreset() {
        Random eqrdmzr = new Random();
        randomNumber = eqrdmzr.nextInt(5) + 1;

            if (randomNumber == 1) {
                equation = 2n + 7 = 17
            }

            if(randomNumber == 2) {
                equation = 2n – 18 = 4
            }

            if(randomNumber == 3) {
                equation = 3n – 5 = 19
            }

            if(randomNumber == 4) {
                iCounterCoeff = 3n + 8 = 95
            }

            if(randomNumber == 5) {
                equation = 3n + 11 = 62
            }

        //Build scene1 - top BorderPane
        isoltext = new Text("Isolate the Variable Term");

        //Build scene1 - center BorderPane
        -- Equation is inserted here --

        //Build scene1 - bottom BorderPane
        -- User controls for isolating the variable term and advancing to next scene--

        //Create GridPanes and Fill Them
        gridpane1 contains isoltext
        gridpane2 contains the equation
        gridpane3 contains the user controls

        //Add GridPane to BorderPane
        gridpanes are added to borderpane

        //Add BorderPane to Scene
        scene2 = new Scene(borderpane, 500, 300);

        solve();
    }

    public void isolcontinuebtn_Click() {
        stage.setScene(scene3);
    }

    public void solve() { 
        //Build scene3 – top BorderPane
        slvtext = new Text("Solve for the Variable");

        //Build scene3 - center BorderPane
        -- Equation is inserted here --

        //Build scene3 - bottom BorderPane
        -- User controls for solving for the variable and advancing to next scene --

        //Create GridPanes and Fill Them
        Gridpane4 contains slvtext
        Gridpane5 contains the equation
        Gridpane6 contains the user controls

        //Add GridPanes to BorderPane
        gridpanes are added to borderpane2

        //Add BorderPane to Scene
        scene3 = new Scene(slvborderpane, 500, 300);

        sbmt();
    }

    private void slvsbmtbtn_Click() {
        if (iCounterSolve1b == 1 && iCounterSolve1a == iCounterSolve2a) {
            stage.setScene(scene4);
        }
    }

    public void sbmt() {
        //Build scene4
        sbmttext = new Text("Correct! Would You Like to Try Another?");

        yesbtn = new Button("Yes");
        yesbtn.setStyle("-fx-font-size: 12pt;");
        yesbtn.setOnAction(e -> yesbtn_Click());

        nobtn = new Button("No");
        nobtn.setStyle("-fx-font-size: 12pt;");
        nobtn.setOnAction(e -> nobtn_Click());

        sbmt = new HBox(30);
        sbmt.setPadding(new Insets(0, 0, 30, 0));
        sbmt.setAlignment(Pos.CENTER);
        sbmt.getChildren().addAll(yesbtn, nobtn);

        //Create GridPanes and fill them
        sbmtgridpane1 = new GridPane();
        sbmtgridpane1.setAlignment(Pos.CENTER);
        sbmtgridpane1.add(sbmttext, 0, 0);

        sbmtgridpane2 = new GridPane();
        sbmtgridpane2.setAlignment(Pos.CENTER);
        sbmtgridpane2.add(sbmt, 0, 0);

        sbmtborderpane = new BorderPane();
        sbmtborderpane.setTop(sbmtgridpane1);
        sbmtborderpane.setCenter(sbmtgridpane2);

    scene4 = new Scene(borderpane3, 500, 300);
    }

    public void yesbtn_Click() {
        stage.setScene(scene2);
    }

    public void nobtn_Click() {
        stage.close();
    }

}




Aucun commentaire:

Enregistrer un commentaire