【问题标题】:Button Click to Send User to Start of Program按钮单击以将用户发送到程序开始
【发布时间】:2015-05-28 20:04:11
【问题描述】:

我正在开发一个方程式程序,它会随机选择 50 个方程式中的一个,然后让用户通过一系列场景来求解它。一旦用户解出方程,就会询问他们是否需要另一个方程。如果他们回答“否”,则程序关闭。如果他们回答是,程序应该随机选择另一个方程,然后带他们通过场景来解决那个问题。

该程序在第一次运行时就像我希望的那样运行。但是,如果用户为另一个方程选择“是”,程序会显示第一个场景的 END,向他们展示他们已经解决的上一个问题。

如何将用户带到场景的开头,以便随机选择一个新方程?

这里是场景一的相关代码:

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

【问题讨论】:

  • 我只是想澄清一些疑问,您的目标是根据选择更改方程式问题,所以如果这是您更改场景的原因,而不是只有一个场景整个过程并更改其中的内容。
  • @Hareesh - 老实说,我正在做我在互联网上看到的其他示例。我怀疑这是我的问题,但是我尝试过的所有其他方法(例如 do...while 循环,并尝试将其发送到方法的开头)都没有奏效。 “全过程一个场景,改变里面的内容。”你能给我一个例子来说明你的意思吗?谢谢。

标签: java button random javafx restart


【解决方案1】:

只是在舞台上设置场景并不会重新加载场景的内容..

如何解决这个问题..?

据我所知,你不需要改变场景。创建一个名为loadMainDisplay() 的简单方法,该方法通过向其添加带有所有必需控件的网格来创建BorderPane isolBorderPane

BorderPane loadMainDisplay() {
    ...
}

您可以在加载内容时首先调用它。稍后,当用户为另一个方程选择YES 时,再次调用此方法。

yesBtn.setOnAction(event -> {
    if (event.getSource() == yesBtn) {
          scene.setRoot(loadMainDisplay());
    }
});

【讨论】:

  • 感谢您的回答。很抱歉,但我并没有完全关注你:我是用loadMainDisplay() 方法代替BorderPane isolBorderPane,还是在代码中的某处添加它(如果是,在哪里?)。另外,我是添加一次还是两次(一次用于初始运行,再次用于“是”选项)?最后,你能告诉我这个方法是什么样子的吗?我是只添加isolBorderPane,还是同时添加isolGridPanes? (我很抱歉,但我对编程还是很陌生,并且不像已经这样做了一段时间的人那么直观)?谢谢!
【解决方案2】:
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;
}

 }

这是在场景中更改窗格的一个示例。 不建议更改整个场景。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多