JavaFX MVC控制器无法正常工作

时间:2015-10-30 13:40:44

标签: model-view-controller javafx controller javafx-2

我是javaFX的初学者,我尝试使用MVC开发应用程序。在这个应用程序中,我使用两个控制器,一个用于menuBar,另一个用于包含按钮和标签的panelCenter。当我点击按钮(CenterControler)"它工作"但是当我点击File-> Quit(MainControler)时,它不起作用。

这里的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 个答案:

答案 0 :(得分:1)

注入控制器的字段的名称是通过获取fx:id元素的fx:include属性的值并连接"Controller"来构造的。因此,注入嵌套控制器的字段名称必须为p2Controller而不是centreControle

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

    //...
}

不幸的是,没有详细记录......(我必须查看源代码才能找到)