我正在创建一个应用程序,并希望根据用户输入的数字用一个按钮更改屏幕。例如,如果用户输入" 1234",程序将检查另一个类以查看该数字是否存在。如果是,则继续使用SceneOne。如果用户输入的数字与其他类中的数字相反,则转到SceneTwo。但是,当我尝试将位置设置为不同的场景时,它会忽略它并自动将我发送到第一个屏幕。
Storage.Java(包含预定义的数字)
int pin = 1234;
public int getPin() {
return number;
}
我的另一个类:(按下指定按钮时执行的功能)
public void switchScenes(ActionEvent e) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/application/ScreenOne.fxml"));
Parent changeScenes = loader.load();
Scene firstScene = new Scene(changeScenes);
Stage window = (Stage)((Node)e.getSource()).getScene().getWindow();
Storage a = new Storage();
int pin = a.getPin();
if(Integer.parseInt((screen.getText())) == pin) {
//screen is a field where user can enter a number
window.setScene(firstScene);
window.show();
}
else if(Integer.parseInt(screen.getText()) == reverseNumber(pin)) {
//if number entered is in reverse, change to ScreenTwo
loader.setLocation(getClass().getResource("/application/ScreenTwo.fxml"));
}
答案 0 :(得分:0)
我可以在单个舞台上提供改变场景的格式: 在第六行是所需的.fxml对象。您可以简单地创建一个if-then语句,并根据输入加载您想要的任何.fxml对象。当我想要改变项目中的场景时,这是我所依赖的代码片段。 {
@FXML
public void buttonclicked(ActionEvent event) throws IOException //This method loads a new scene in a current window
{
//note that on this line you can substitue "Screen2.fxml" for a string chosen prior to this line.
Parent loader = FXMLLoader.load(getClass().getResource("Screen2.fxml"));//Creates a Parent called loader and assign it as ScReen2.FXML
Scene scene = new Scene(loader); //This creates a new scene called scene and assigns it as the Sample.FXML document which was named "loader"
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow(); //this accesses the window.
app_stage.setScene(scene); //This sets the scene as scene
app_stage.show(); // this shows the scene
}
}