来自外国控制器的 JavaFX TabPane Tab select
JavaFX TabPane Tab select from foreign Controller
在我根据这个有用的 post (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;
}
}
}
!packages and imports are removed bc of Body length!
I know that I have to rewrite the last few functions as they could easy be one.
Steuerung.fxml 和 Anzeige.fxml 会太长。如果你想看到它们,这里是完整的 GitLab 项目。
GitLab Project
Sorry for the German comments, function/var names and or objects
感谢您的建议!
Edit: Grammar
在Controller1
中创建一个Controller2
实例作为实例变量:
private Controller2 controller2 = new Controller2(this);
并且当用户单击 b_Ecke
标签时,您在该实例中更改选项卡:
b_Ecke.setOnMouseClicked(e -> controller2.setTab(2));
但是,您的代码中没有任何地方可以在 Controller2
的实例上调用 showStage
,因此您正在更改未显示的选项卡选择。
当用户选择 b_AnzeigeÖffnen
菜单项时,您创建一个 new 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();
}
在我根据这个有用的 post (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;
}
}
}
!packages and imports are removed bc of Body length!
I know that I have to rewrite the last few functions as they could easy be one.
Steuerung.fxml 和 Anzeige.fxml 会太长。如果你想看到它们,这里是完整的 GitLab 项目。 GitLab Project
Sorry for the German comments, function/var names and or objects
感谢您的建议!
Edit: Grammar
在Controller1
中创建一个Controller2
实例作为实例变量:
private Controller2 controller2 = new Controller2(this);
并且当用户单击 b_Ecke
标签时,您在该实例中更改选项卡:
b_Ecke.setOnMouseClicked(e -> controller2.setTab(2));
但是,您的代码中没有任何地方可以在 Controller2
的实例上调用 showStage
,因此您正在更改未显示的选项卡选择。
当用户选择 b_AnzeigeÖffnen
菜单项时,您创建一个 new 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();
}