我是编程的新手,过去两周才开始学习,所以我很抱歉任何冗余或草率的代码...
我有两个场景,都在我的主课堂上。但是我使用FXML来开发每个场景,并且所有代码都被放置在第一个场景的FXML控制器中。我已准备好开始构建我的第二个场景,但不知道如何正确启动它。
我的问题是,如何设置舞台以显示第二个场景(mainCallWindow),特别是在第一个FXML文件的控制器类中。如果有赌博的方式,请告诉我。
主类:
package supportTool;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public String versionNumber = "v2.1";
@Override
public void start(Stage primaryStage) throws Exception {
// SETTING UP THE STAGE
Stage window;
window = primaryStage;
window.setTitle("Support Tool " + versionNumber);
// SETTING UP THE SCENES
Parent newCallDetailsFXML = FXMLLoader.load(getClass().getResource("newCallDetails.fxml"));
Parent mainCallWindowFXML = FXMLLoader.load(getClass().getResource("mainCallWindow.fxml"));
Scene newCallDetails = new Scene (newCallDetailsFXML, 800, 600);
Scene mainCallWindow = new Scene (mainCallWindowFXML, 800, 600);
// CHOOSING THE SCENE AND SHOWING THE STAGE
window.setScene(newCallDetails);
window.show();
}
}
场景1 FXML控制器:
package supportTool;
import javafx.scene.control.*;
import javafx.scene.image.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class newCallController {
private int maxChar;
public ChoiceBox choiceAccount;
public ImageView btnCall;
public TextField tfCallbackNumber;
public TextField tfCallerName;
public TextField tfStoreNumber;
// ACTION COMPLETED WHEN CALL BUTTON IS PRESSED
public void btnCall() {
Caller newCaller = new Caller();
newCaller.setCallerName(tfCallerName.getText());
newCaller.setCallbackNumber(tfCallbackNumber.getText());
newCaller.setAccount(String.valueOf(choiceAccount.getValue()));
newCaller.setStoreNumber(tfStoreNumber.getText());
try {
FileOutputStream fos = new FileOutputStream("caller.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(newCaller);
oos.close();
fos.close();
} catch(IOException e){
e.printStackTrace();
}
// RIGHT HERE IS WHERE I WANT TO SET THE SCENE TO "mainCallWindow"
}
// CHECKS TO SEE IF THE TEXT CONTAINS ONLY LETTERS
private boolean isNumberCheckEvent(String message) {
if (message.matches("[0-9]+")) {
return true;
} else {
return false;
}
}
// SETS THE MAX CHARACTERS FOR ALL TEXTFIELDS
public void maxCharEvent() {
// CALLER NAME MAX CHARACTERS
tfCallerName.setOnKeyTyped(maxCharEvent -> {
maxChar = 20;
if(tfCallerName.getText().length() >= maxChar) {
maxCharEvent.consume();
}
});
// CALLBACK NUMBER MAX CHARACTERS
tfCallbackNumber.setOnKeyTyped(maxCharEvent -> {
maxChar = 10;
if(tfCallbackNumber.getText().length() >= maxChar) {
maxCharEvent.consume();
}
});
// STORE NUMBER MAX CHARACTERS
tfStoreNumber.setOnKeyTyped(maxCharEvent -> {
maxChar = 5;
if (String.valueOf(choiceAccount.getValue()).equals("6 Digit Account")) {
maxChar = 6;
}
if (tfStoreNumber.getText().length() >= maxChar) {
maxCharEvent.consume();
}
});
}
// CHANGES TEXT TO ONLY LETTERS BASED ON isNumberCheckEvent
public void numberValidationEvent() {
tfCallbackNumber.setOnKeyReleased(numberValidationEvent -> {
maxCharEvent();
if(tfCallbackNumber.getText().length() > 0) {
if (!isNumberCheckEvent(tfCallbackNumber.getText())) {
tfCallbackNumber.setText(tfCallbackNumber.getText().substring(0, tfCallbackNumber.getText().length() - 1));
tfCallbackNumber.positionCaret(10);
numberValidationEvent.consume();
}
}
});
tfStoreNumber.setOnKeyReleased(numberValidationEvent -> {
maxCharEvent();
if(tfStoreNumber.getText().length() > 0) {
if (!isNumberCheckEvent(tfStoreNumber.getText())) {
tfStoreNumber.setText(tfStoreNumber.getText().substring(0, tfStoreNumber.getText().length() - 1));
tfStoreNumber.positionCaret(10);
numberValidationEvent.consume();
}
}
});
}
}
答案 0 :(得分:0)
您可以通过各种方式更改场景。在你目前的情况下,你可以尝试下面的东西。首先,您需要参考FXMLLoader,场景和舞台来改变您的控制器场景。而不是在主类中加载,在控制器类中加载。
FXMLLoader loader = new FXMLLoader(getClass().getResource("mainCallWindow.fxml"));
Parent mainCallWindowFXML = loader.load();
//use one of components on your scene to get a reference to your scene object.
Stage stage = (Stage)tfCallerName.getScene.getWindow();//or use any other component in your controller
Scene mainCallWindow = new Scene (mainCallWindowFXML, 800, 600);
stage.setScene(newCallDetails);
stage.show(); //this line may be unnecessary since you are using the same stage.
}
这不是实现这一目标的唯一方法。您可以使用相同的场景加载不同的FXML文件。我建议更改场景的根节点,而不是完全改变场景。