在此有用的帖子(PASSING PARAMETERS JAVAFX FXML)的引导下更改了代码之后,我的setTab函数无法正常工作。它被调用但没有执行应做的事情。

Main.java

public class Main extends Application {

public static void main(String[] args) {
    launch(args);
}
@Override
public void start(Stage primaryStage){
        Controller1 controller1 = new Controller1();
        controller1.showStage();
  }
}


Controller1.java

public class Controller1{

    private final Stage thisStage;
    private Controller2 controller2 = new Controller2(this);

    public Controller1() {
    thisStage = new Stage();

    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Steuerung.fxml"));
        loader.setController(this);
        thisStage.setScene(new Scene(loader.load()));
        thisStage.setTitle("Steuerung");
    }catch (IOException e){
        e.printStackTrace();
     }
    }

    public void showStage(){ thisStage.show();}

    @FXML private void initialize() {
        b_AnzeigeÖffnen.setOnAction(e -> openAnzeige());
        b_Ecke.setOnMouseClicked(e -> controller2.setTab(2));
    }

    private void openAnzeige(){
        Controller2 controller2 = new Controller2(this);
        controller2.showStage();
    }

    @FXML
    private Label b_Ecke;

    @FXML
    private MenuItem b_AnzeigeÖffnen

}


Controller2.java

public class Controller2 {

private Stage thisStage;

private final Controller1 controller1;

public Controller2(Controller1 controller1) {
    this.controller1 = controller1;
    thisStage = new Stage();
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Anzeige.fxml"));
        loader.setController(this);
        thisStage.setScene(new Scene(loader.load()));
        thisStage.setTitle("Spielstand");

        }catch (IOException e){
            e.printStackTrace();
        }
}

public void showStage() { thisStage.show();}


@FXML
private TabPane TP_A;

@FXML
private Tab tA_Home;

@FXML
private Tab tA_EckePreview;

@FXML
private Tab tA_EckVerhältnis;

@FXML
private Tab tA_Spielverlauf;

@FXML
private Tab tA_Spielstatistik;

@FXML
private Tab tA_GelbeKarte;

@FXML
private Tab tA_RoteKarte;

@FXML
private Tab tA_Elfmeter;

@FXML
private Tab tA_Auswechselung;


@FXML public void setTab(Integer i) {
   System.out.println("TABSWITCH");
    switch(i) {
        case 1: TP_A.getSelectionModel().select(tA_Home); break;
        case 2: TP_A.getSelectionModel().select(tA_EckePreview); break;
        case 3: TP_A.getSelectionModel().select(tA_EckVerhältnis); break;
        case 4: TP_A.getSelectionModel().select(tA_Spielverlauf); break;
        case 5: TP_A.getSelectionModel().select(tA_Spielstatistik); break;
        case 6: TP_A.getSelectionModel().select(tA_GelbeKarte); break;
        case 7: TP_A.getSelectionModel().select(tA_RoteKarte); break;
        case 8: TP_A.getSelectionModel().select(tA_Elfmeter); break;
        case 9: TP_A.getSelectionModel().select(tA_Auswechselung); break;
        //case 10: TP_A.getSelectionModel().select(tA_Nachspielzeit/Verlängerung); break; -> TODO
        default: System.out.println("Fehler: Tab in TP_A nicht vorhanden!"); break;
        }
    }
}



!包装和进口件的车身长度已删除!

我知道我必须重写最后几个函数,因为它们很容易合而为一。


Steuerung.fxml和Anzeige.fxml太长了。如果您想看到它们,这是完整的GitLab项目。
GitLab Project


对不起,德语注释,函数/变量名和或对象


感谢您的建议!


编辑:语法

最佳答案

Controller1中,创建一个Controller2实例作为实例变量:

private Controller2 controller2 = new Controller2(this);


当用户单击b_Ecke标签时,您可以在该实例中更改选项卡:

b_Ecke.setOnMouseClicked(e -> controller2.setTab(2));


但是,在代码中没有任何地方可以在showStage的实例上调用Controller2,因此您正在更改选项卡中未显示的内容。

当用户选择b_AnzeigeÖffnen菜单项时,您将创建一个新的Controller2实例,并显示该实例:

b_AnzeigeÖffnen.setOnAction(e -> openAnzeige());

private void openAnzeige(){
    Controller2 controller2 = new Controller2(this);
    controller2.showStage();
}


相反,仅显示您已经创建的Controller2实例:

b_AnzeigeÖffnen.setOnAction(e -> openAnzeige());

private void openAnzeige(){
    // Controller2 controller2 = new Controller2(this);
    controller2.showStage();
}

10-08 17:21