【问题标题】:javaFx aplication's start methodjavaFx 应用程序的启动方法
【发布时间】:2018-06-06 09:30:32
【问题描述】:

伙计们,我有一个问题,我的代码是 Game Of 15 的示例,当时我展示了如图所示的洗牌场景

enter image description here

现在我想给用户移动并更改场景并再次显示它直到游戏结束但是当我在 PrimaryStage.show() 之后放置时它崩溃了所以我的问题是如何更改场景并再次显示它.

public class Q3 extends Application {
static Label lab[] ;
static Rectangle rec[] ;
static int X = 3,Y = 3;
static  int[] arr = new int[16];
public static void main(String[] args) {launch(args);
}

@Override
public void start(Stage primaryStage) throws IOException,terruptedException {
    Scanner in = new Scanner(System.in);
    AnchorPane root = new AnchorPane();
    Scene myScene = new Scene(root,301,301);
    rec = new Rectangle[15];
    lab = new Label[15];
    int x = 0,y = 0,Fit = 0;
    arr[15] = 0;
    for(int i = 0;i < 15; i++){
        rec[i] = new Rectangle(20,20,75,75);
        lab[i] = new Label(Integer.toString(i+1));
        rec[i].setEffect(new InnerShadow());
        if(x > 3) {
            x = 0;
            y++;
        }
        Fit = (i < 9) ? 2 : 0;
        lab[i].setTextFill(Color.PERU);
        lab[i].setLayoutX(35 + x * 75 + Fit );
        lab[i].setLayoutY(31 + y * 75 + Fit );
        arr[i] = i;
        rec[i].setX( 1 + x++ * 75);
        rec[i].setY( 1 + y * 75);
        rec[i].setAccessibleText(Integer.toString(i));
        rec[i].setStroke(Color.OLIVE);
        rec[i].setFill(Color.LIGHTBLUE);
        root.getChildren().addAll(rec[i],lab[i]);
    }
    primaryStage.setTitle("The Game Of 15");
    primaryStage.setResizable(true);
    primaryStage.setScene(myScene);
    for(int i = 0;i < 1000;i++){
        shuffle();
    }
    primaryStage.show();
    while(!isfinished()){
       char c = in.next().trim().charAt(0);
       if(c == 'w'){
           do something
       }
       else if ...
    }
}

【问题讨论】:

  • 这根本不是 JavaFX 的工作方式:它是事件驱动的。如果您阻止 FX 应用程序线程,它将无法更新 UI。在 GUI 应用程序中,无论如何从标准输入中读取都没有任何意义。 GUI 应用程序的全部意义在于用户与 UI 中的控件进行交互。您应该编写处理用户事件的事件处理程序。 (例如,也许您在创建的图块上有鼠标侦听器等)。

标签: java javafx


【解决方案1】:

以下是 javafx 中如何切换的示例代码:

public class SwitchingScene extends Application {

    public static void main(String[] args) {
        launch(args);
    }
Stage stage;
    @Override
    public void start(Stage window) throws Exception {
        VBox layout1= new VBox(20);
        Button b1=  new Button("Goto Scene2");
        Button b2=  new Button("Goto Scene1");
        StackPane layout2= new StackPane();
        layout2.getChildren().add(b2);
        Scene scene2= new Scene(layout2,200,200);
        Scene scene1= new Scene(layout1,300,400);
        b1.setOnAction(e-> window.setScene(scene2));
        b2.setOnAction(e-> window.setScene(scene1));
        layout1.getChildren().addAll(new Label("Scene1"),b1);
        window.setScene(scene1);
        window.setTitle("My Window");
        window.show();

    }

}

我现在无法通过你的代码来理解你做了什么,但你可以参考这段代码来看看切换是如何工作的。在按钮动作监听器上我已经完成了切换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2018-05-07
    • 2022-01-15
    • 2021-01-13
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多