2015-10-30 96 views
2

我是javaFX的初學者,我嘗試用MVC開發應用程序。在這個應用程序中,我使用兩個控制器,一個用於menuBar,另一個用於包含按鈕和標籤的panelCenter。當我點擊按鈕(CenterControler)「它工作」,但是當我點擊文件 - >退出(MainControler)它不起作用。JavaFX MVC控制器不工作

這裏,文件Main.java:

package application; 
import java.io.IOException; 
import controler.CenterControler; 
import controler.MainControler; 
import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.stage.Stage; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 

public class Main extends Application { 
    CenterControler ctrl_Center; 
    MainControler ctrl_Main; 
    Stage primaryStage; 
    @Override 
    public void start(Stage primaryStage) throws IOException { 
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/themain.fxml")); 
    Parent root = (Parent) fxmlLoader.load(); 
    ctrl_Main = (MainControler)fxmlLoader.getController(); 
    Scene scene = new Scene(root, 800, 600); 
    primaryStage.setTitle("FXML Welcome"); 
    primaryStage.setScene(scene); 
    primaryStage.show();} 

    public static void main(String[] args) { 
      launch(args); 
     } 
    } 

MainControler.java

package controler; 
import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Menu; 
import javafx.scene.control.MenuBar; 
import javafx.scene.control.MenuItem; 

public class MainControler implements Initializable{ 

    @FXML 
    private Menu File; 

    @FXML 
    private MenuBar barreMenu; 

    @FXML 
    private Menu sousMenu; 

    @FXML 
    private MenuItem menuQuit; 

    @FXML 
    private CenterControler centreControle; 


    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     assert File != null : "fx:id=\"File\" was not injected: check your FXML file 'themain.fxml'."; 
     assert menuQuit != null : "fx:id=\"menuQuit\" was not injected: check your FXML file 'themain.fxml'."; 
    } 

    public void setCenterControle(CenterControler cc){this.centreControle = cc; } 

    @FXML 
    protected void onClick2() 
    { 
     centreControle.show("IT WORKS !!! FROM MAIN CONTROLER"); 
    } 
} 

CenterControler.java

package controler; 
import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.AnchorPane; 

public class CenterControler implements Initializable{ 

    @FXML 
    private AnchorPane monFonds; 

    @FXML 
    private Button btn_1; 

    @FXML 
    private Label labelMSG; 

    @Override 
    public void initialize(URL arg0, ResourceBundle arg1) { 
     assert btn_1 != null : "fx:id=\"btn_1\" was not injected: check your FXML file 'part2.fxml'."; 
     assert labelMSG != null : "fx:id=\"labelMSG\" was not injected: check your FXML file 'part2.fxml'."; 
    } 

    public void setBTN(Button btn){ this.btn_1 = btn;} 

    public void setLabel(Label lb) {this.labelMSG = lb; } 

    public Button getBTN(){ return this.btn_1; } 

    public Label getLabel(){ return this.labelMSG;} 

    @FXML 
    protected void onClickShowMSG() 
    { 
     labelMSG.setText("PUSH FROM CENTER CONTROLER"); 
    } 

    public void show(String msg) 
    { 
     labelMSG.setText(msg); 
    } 
} 

themain.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.paint.*?> 
<?import javafx.scene.text.*?> 

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler.MainControler"> 
    <children> 
     <MenuBar fx:id="barreMenu" VBox.vgrow="NEVER"> 
      <menus> 
       <Menu fx:id="sousMenu" mnemonicParsing="false" text="File"> 
      <items> 
      <MenuItem fx:id="menuQuit" mnemonicParsing="false" onAction="#onClick2" text="Quit" /> 
      </items> 
     </Menu> 
     </menus> 
    </MenuBar> 
     <fx:include fx:id="p2" source="part2.fxml" /> 
    </children> 
</VBox> 

part2.fxml提前

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

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler.CenterControler"> 
    <children> 
     <Button fx:id="btn_1" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#onClickShowMSG" text="Button" /> 
     <Label fx:id="labelMSG" layoutX="286.0" layoutY="128.0" text="Label" /> 
    </children> 
</AnchorPane> 

感謝。

回答

1

通過取fx:include元素的fx:id屬性的值和連接"Controller"來構造控制器注入的字段的名稱。因此,在嵌套控制器注入的字段名稱必須p2Controller而不是centreControle

public class CenterControler implements Initializable { 
    //... 
    @FXML 
    private CenterControler p2Controller; 

    //... 
} 

不幸的是,這是不是有據可查的......(我不得不看的源代碼,找出)

+0

非常感謝您的工作! – MrBeginnerCoder

+0

記錄,但非常糟糕,[在這裏](http://docs.oracle.com/javase/8/javafx/api/javafx/fxml/doc-files/introduction_to_fxml.html#nested_controllers)。 –