我有一个我正在研究的方程式程序,它随机选择50个方程式中的一个,然后让用户通过一系列场景来解决它。一旦用户解决了等式,他们就会问他们是否想要另一个等式。如果他们回答否,程序将关闭。如果他们回答是,程序应该随机选择另一个等式,然后通过场景来解决那个。
该程序正如我第一次想要的那样工作。但是,如果用户选择"是"对于另一个等式,程序显示第一个场景的END,向他们显示他们已经解决的上一个问题。
如何将用户发送到场景的开头,以便随机选择新的等式?
以下是场景1的相关代码:
package Equations;
import java.util.Random;
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
public class equationsapp extends Application
implements EventHandler<ActionEvent> {
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage primaryStage) {
stage = primaryStage;
Random eqrdmzr = new Random();
int randomNumber = eqrdmzr.nextInt(3) + 1;
if (randomNumber == 1) {
isolCounterCoeff = 2;
isolVrblb = new Label("+");
isolCounter1a = 7;
isolCounter2a = 17;
slvCoeff = 2;
slvEqVrblTerm = new Text("2n");
slvEqWhlNmbrInt = 10;
slvEqWhlNmbr = new Text("10");
}
if(randomNumber == 2) {
isolCounterCoeff = 2;
isolVrblb = new Label("+");
isolVrblb.setVisible(false);
isolCounter1a = -18;
isolCounter2a = 4;
slvCoeff = 2;
slvEqVrblTerm = new Text("2n");
slvEqWhlNmbrInt = 22;
slvEqWhlNmbr = new Text("22");
}
if(randomNumber == 3) {
isolCounterCoeff = 3;
isolVrblb = new Label("+");
isolVrblb.setVisible(false);
isolCounter1a = -5;
isolCounter2a = 19;
slvCoeff = 3;
slvEqVrblTerm = new Text("3n");
slvEqWhlNmbrInt = 24;
slvEqWhlNmbr = new Text("24");
}
//Build Scene 1 - Top BorderPane
Text isolText = new Text("Isolate the Variable Term");
isolText.setStyle("-fx-font-size: 16pt");
//Build Scene 1 - Center BorderPane
Label isolCoeff = new Label();
isolCoeff.setStyle("-fx-font-size: 24pt;");
isolCoeff.setText(Integer.toString(isolCounterCoeff));
Label isolVrbl = new Label("n");
isolVrbl.setStyle("-fx-font-size: 24pt;");
isolVrblb.setStyle("-fx-font-size: 24pt;");
isolVrblb.managedProperty().bind(isolVrblb.visibleProperty());
Label isolEqIntLeft = new Label();
isolEqIntLeft.setStyle("-fx-font-size: 24pt;");
isolEqIntLeft.setPadding(new Insets(0, 10, 0, 0));
isolEqIntLeft.setText(Integer.toString(isolCounter1a));
isolEqIntLeft.managedProperty().bind(isolEqIntLeft.visibleProperty());
Label isolEqualSign = new Label("=");
isolEqualSign.setStyle("-fx-font-size: 24pt;");
Label isolEqIntRight = new Label();
isolEqIntRight.setStyle("-fx-font-size: 24pt;");
isolEqIntRight.setPadding(new Insets(0, 0, 0, 10));
isolEqIntRight.setText(Integer.toString(isolCounter2a));
//Build Scene 1 - Bottom BorderPane
Label isolLbl1 = new Label();
isolLbl1.setStyle("-fx-font-size: 22pt;");
isolEqIntLeft.setText(Integer.toString(isolCounter1a));
isolLbl1.setText(Integer.toString(isolCounter1b));
//Create GridPanes and Fill Them
GridPane isolGridPane1 = new GridPane();
isolGridPane1.setAlignment(Pos.CENTER);
isolGridPane1.add(isolText, 0, 0);
GridPane isolGridPane2 = new GridPane();
isolGridPane2.setAlignment(Pos.CENTER);
isolGridPane2.add(isolCoeff, 0, 0);
isolGridPane2.add(isolVrbl, 1, 0);
isolGridPane2.add(isolVrblb, 2, 0);
isolGridPane2.add(isolEqIntLeft, 3, 0);
isolGridPane2.add(isolEqualSign, 4, 0);
isolGridPane2.add(isolEqIntRight, 5, 0);
GridPane isolGridPane3 = new GridPane();
isolGridPane3.setAlignment(Pos.CENTER);
isolGridPane3.setHgap(25.0);
isolGridPane3.setVgap(10.0);
isolGridPane3.setPadding(new Insets(0, 0, 20, 0));
isolGridPane3.add(isolbtn1, 0, 0);
isolGridPane3.add(isolLbl1, 1, 0);
isolGridPane3.add(isolBtn2, 2, 0);
isolGridPane3.add(isolBtn3, 4, 0);
isolGridPane3.add(isolLbl2, 5, 0);
isolGridPane3.add(isolBtn4, 6, 0);
isolGridPane3.add(isolContinueBtn, 3, 1);
//Add GridPane to BorderPane
BorderPane isolBorderPane = new BorderPane();
isolBorderPane.setTop(isolGridPane1);
isolBorderPane.setCenter(isolGridPane2);
isolBorderPane.setBottom(isolGridPane3);
//Add BorderPane to Scene
scene1 = new Scene(isolBorderPane, 500, 300);
//Add the scene to the stage, set the title and show the stage
primaryStage.setScene(scene1);
primaryStage.setTitle("Equations");
primaryStage.show();
这是应该将它们发送回第1阶段开头的事件处理程序:
Button yesBtn = new Button("Yes");
yesBtn.setStyle("-fx-font-size: 12pt;");
yesBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle (ActionEvent event) {
if (event.getSource() == yesBtn) {
stage.setScene(scene1);
}
}
});
答案 0 :(得分:0)
只是在舞台上设置场景并不重新加载场景的内容..
如何解决此问题?
据我所知,你不需要改变场景。创建一个名为loadMainDisplay()
的简单方法,通过向所有必需控件添加网格来创建BorderPane isolBorderPane
。
BorderPane loadMainDisplay() {
...
}
您可以在加载内容时最初调用它。之后,当用户为另一个等式选择YES
时,再次调用此方法。
yesBtn.setOnAction(event -> {
if (event.getSource() == yesBtn) {
scene.setRoot(loadMainDisplay());
}
});
答案 1 :(得分:0)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ChangePaneExample extends Application{
/**
* @param args
*/
public static void main( String[] args ){
launch( args );
}
int screenNumber = 1;
private GridPane root;
private Scene rootScene;
private StackPane changingPane;
/**
* @see javafx.application.Application#start(javafx.stage.Stage)
* @param primaryStage
* @throws Exception
*/
@Override
public void start( Stage primaryStage ) throws Exception{
root = new GridPane();
rootScene = new Scene( root );
primaryStage.setScene( rootScene );
changingPane = new StackPane();
changeScreen();
Button changeBtn = new Button();
changeBtn.setText( "Change Screen" );
changeBtn.setOnAction( new EventHandler<ActionEvent>(){
@Override
public void handle( ActionEvent arg0 ){
changeScreen();
}
} );
root.addRow( 1, changeBtn );
root.addRow( 2, changingPane );
primaryStage.show();
}
/**
*/
private void changeScreen(){
if( screenNumber > 2 ) screenNumber = 1;
changingPane.getChildren().clear();
changingPane.getChildren().add( getDisplayPane( screenNumber + "" ) );
screenNumber++;
}
public static Pane getDisplayPane( String uniqueIdOfScreen ){
switch( uniqueIdOfScreen ){
case "1":
return getIsoletedGridPane2();
case "2":
return getIsoletedGridPane1();
default:
break;
}
return null;
}
public static Pane getIsoletedGridPane2(){
GridPane isolGridPane3 = new GridPane();
Label label = new Label();
label.setText( "this is isolated GridPane--------------- 2 ----------------------" );
isolGridPane3.getChildren().add( label );
return isolGridPane3;
}
public static Pane getIsoletedGridPane1(){
HBox isolGridPane3 = new HBox();
Label label = new Label();
label.setText( "this is isolated HBox --------------------------- 1 ----------------------------" );
isolGridPane3.getChildren().add( label );
return isolGridPane3;
}
}
这是更改场景中窗格的一个示例。 建议不要改变整个场景。