2014-10-27 156 views
0

你好,我从JavaFX开始,我遇到了一些问题。 我想从两个diferents的意见沟通两个控制。我该怎么做? 我使用标签和我有这两个控制器和我想要做类似这样的:JavaFX。控制器之间的通信

Application.java:

public class JavaFXApplication6 extends Application 
{ 

@Override 
public void start(Stage primaryStage) 
{ 
    try 
    { 
     Parent root = FXMLLoader.load(getClass().getResource(
       "/view/FXML_Main.fxml")); 
     Scene scene = new Scene(root); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } catch(Exception e){ 
     e.printStackTrace(); 
    } 

    primaryStage.setTitle("Back-end GUI"); 
} 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) 
{ 
    launch(args); 
} 

} 

FXML_Main.fxml:

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
    xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" 
    fx:controller="controller.FXML_MainController"> 
    <children> 
     <TabPane layoutX="61.0" layoutY="30.0" prefHeight="400.0" prefWidth="600.0" 
     tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" 
     AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" 
     AnchorPane.topAnchor="0.0"> 
     <tabs> 
      <Tab text="Untitled Tab 1"> 
       <content> 
        <fx:include source="FXML_Tab1.fxml" /> 
       </content> 
      </Tab> 
      <Tab text="Untitled Tab 2"> 
       <content> 
        <fx:include source="FXML_Tab2.fxml" /> 
       </content> 
      </Tab> 
     </tabs> 
     </TabPane> 
    </children> 
</AnchorPane> 

FXML_MainController.java:

public class FXML_MainController implements Initializable { 

/** 
* Initializes the controller class. 
*/ 
@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
}  

} 

FXML_Tab1.fx毫升:

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="controller.FXML_Tab1Controller"> 
<children> 
    <Label fx:id="Label1" layoutX="282.0" layoutY="108.0" text="Label" /> 
    <TextField fx:id="FextField1" layoutX="215.0" layoutY="146.0" /> 
    <Button fx:id="Button1" layoutX="269.0" layoutY="197.0" mnemonicParsing="false" 
     onAction="#actionButton1" text="Button" /> 
</children> 
</AnchorPane> 

FXML_Tab1Controller.java:

public class FXML_Tab1Controller implements Initializable { 

FXML_Tab2Controller tab2controller; 

@FXML public Label Label1; 
@FXML public TextField TextField1; 
@FXML public Button Button1; 
/** 
* Initializes the controller class. 
*/ 
@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
} 

@FXML private void actionButton1(ActionEvent event) 
{ 
    Label1.setText(tab2controller.TextField2.getText()); 
} 

} 

FXML_Tab2.fxml:

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" 
    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
    fx:controller="controller.FXML_Tab2Controller"> 
    <children> 
     <Label fx:id="Label2" layoutX="282.0" layoutY="99.0" text="Label" /> 
     <TextField fx:id="TextField2" layoutX="215.0" layoutY="149.0" /> 
     <Button fx:id="Button2" layoutX="270.0" layoutY="200.0" mnemonicParsing="false" 
    onAction="#actionButton2" text="Button" /> 
    </children> 
</AnchorPane> 

FXML_Tab2Controller.java:

public class FXML_Tab2Controller implements Initializable { 

FXML_Tab1Controller tab1controller; 
@FXML public Label Label2; 
@FXML public TextField TextField2; 
@FXML public Button Button2; 
/** 
* Initializes the controller class. 
*/ 
@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
} 

@FXML private void actionButton2(ActionEvent event){ 
     Label2.setText(tab1controller.TextField1.getText()); 
} 

} 

类似这样的东西视频: https://www.youtube.com/watch?v=XLVx46ycxco

回答

4

添加fx:id到每个<fx:include>标签:

<tabs> 
     <Tab text="Untitled Tab 1"> 
      <content> 
       <fx:include source="FXML_Tab1.fxml" fx:id="tab1" /> 
      </content> 
     </Tab> 
     <Tab text="Untitled Tab 2"> 
      <content> 
       <fx:include source="FXML_Tab2.fxml" fx:id="tab2" /> 
      </content> 
     </Tab> 
    </tabs> 

这将允许你在相应的控制器注入到你的FXML_MainController

public class FXML_MainController { 

    @FXML 
    private FXML_Tab1Controller tab1Controller ; 
    @FXML 
    private FXML_Tab2Controller tab2Controller ; 

} 

变量命名是非常重要的在这里:这些字段必须命名为xController,其中x是01中的fx:id属性的值。详情请参阅Introduction to FXML documentation

现在,在你的主控制器的initialize()方法,你可以建立两个控制器之间的关系:

public class FXML_MainController { 

    @FXML 
    private FXML_Tab1Controller tab1Controller ; 
    @FXML 
    private FXML_Tab2Controller tab2Controller ; 

    public void initialize() { 
     tab1Controller.tab2Controller = tab2Controller ; 
     tab2Controller.tab1Controller = tab1Controller ; 
    } 
} 
+2

正如一个评论,我真的不建议共享控制器是这样的:你应该在封装数据一个使用JavaFX可观察属性的数据模型,并在两个控制器之间共享数据模型。这为您的控制器提供了更好的解耦。 – 2014-10-27 15:31:12

+0

我尝试做第一件事,但它不起作用。 – Charlio 2014-10-28 10:02:39

+0

详细说明“不起作用” – 2014-10-28 10:38:25