现在我想从用户那里获得动作并更改场景并再次显示它直到游戏结束但是当我在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 ...
}
}
答案 0 :(得分:-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();
}
}
我现在无法通过您的代码来了解您所做的事情,但您可以参考此代码来了解切换的工作原理。在按钮动作侦听器上,我完成了切换。