第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

多個(gè)帶有控制器的FXML,共享對象

多個(gè)帶有控制器的FXML,共享對象

慕勒3428872 2019-11-12 12:41:31
各位晚上好,我已經(jīng)找到了大量關(guān)于此主題的帖子,但是我仍然無法設(shè)法將對象從Controller1傳遞到Controller2。是否有某個(gè)完整的教程或一些示例項(xiàng)目可以做到這一點(diǎn)?我已經(jīng)走了這么遠(yuǎn),直到被卡?。簢壹塸ublic class Country {private SimpleStringProperty country = new SimpleStringProperty("");//Constructorpublic Country() {}//GETTERSpublic String getCountry() {    return country.get();}//SETTERSpublic void setCountry(String value) {    country.set(value);}@Overridepublic String toString() {    return getCountry();}}程序啟動(dòng)時(shí),將加載主FXML(Sample.fxml)。它包含一個(gè)邊框窗格,該窗格的頂部是菜單欄,中間是內(nèi)容窗格。在初始化時(shí),我創(chuàng)建一個(gè)新的Country對象并將其存儲在全局變量中。當(dāng)單擊菜單項(xiàng)時(shí),我有一種將另一個(gè)FXML加載到內(nèi)容窗格中的方法:SampleController.javapublic class SampleController implements Initializable {@FXMLprivate Pane pContent;private Country c;@FXMLprivate void handleButtonAction(ActionEvent event) throws IOException {    System.out.println(c); //this prints Belgium, which is correct    URL url = getClass().getResource("Sub1.fxml");    FXMLLoader fxmlloader = new FXMLLoader();    fxmlloader.setLocation(url);    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());    pContent.getChildren().clear();    pContent.getChildren().add((Node) fxmlloader.load(url.openStream()));}@Overridepublic void initialize(URL url, ResourceBundle rb) {    c = new Country();    c.setCountry("Belgium");}public Country getCountryFromSampleController(){    return c;}}現(xiàn)在,我希望在Sub1.fxml加載時(shí)捕獲Country對象,這意味著我需要在initialize()上獲取country對象:Sub1Controller.javapublic class Sub1Controller implements Initializable {/** * Initializes the controller class. */@Overridepublic void initialize(URL url, ResourceBundle rb) {    SampleController sp = new SampleController(); //I don't know how to fetch the original SampleController object    System.out.println(sp.getCountryFromSampleController());     //this prints null, which is ofcourse logical because I make a new SampleController object.         }    }我有一個(gè)問題,如何獲取“原始” SampleController對象,以便可以使用getCountryFromRoot()方法來獲取具有比利時(shí)值的Country對象?我一直在這個(gè)問題上搜索了好幾個(gè)小時(shí),并閱讀了有關(guān)StackOverflow的每篇文章,但是看來我找不到丟失的鏈接...感謝任何幫助(最好是使用此代碼)!很長的帖子,很抱歉,我試圖盡可能完整,否則我將永遠(yuǎn)無法理解...
查看完整描述

3 回答

?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

FXML是MVC模式的一種簡單形式。FXML文件是一個(gè)視圖,Controller很明顯,錯(cuò)過了什么?模型-一個(gè)用于存儲相對于當(dāng)前視圖的所有數(shù)據(jù)的位置,因此可以在控制器之間共享國家/地區(qū)數(shù)據(jù)。


1.引入模型的一種可能方法是“上下文”。讓我們考慮一個(gè)案例,那么整個(gè)項(xiàng)目只有一個(gè)模型,因此可以以Singleton的形式擁有全局上下文


public class Context {

    private final static Context instance = new Context();


    public static Context getInstance() {

        return instance;

    }


    private Country country = new Country();


    public Country currentCountry() {

        return country;

    }

}

您的SampleController將進(jìn)行以下更改:


@Override

public void initialize(URL url, ResourceBundle rb) {

    Context.getInstance().currentCountry().setCountry("Belgium");

}

并SubController1可以以相同的方式訪問它:


@Override

public void initialize(URL url, ResourceBundle rb) {

    System.out.println(Context.getInstance().currentCountry().getCountry());

}

2.另一種方法是將上下文傳遞給SubController1您,然后加載它的xml。如果您不想擁有應(yīng)用程序全局模型,它將更好地工作。因此,創(chuàng)建類似的Context類,但不包含實(shí)例字段,并且:


public class Sub1Controller implements Initializable {

    private Context context;

    public void setContext(Context context) {

        this.context = context;

        // initialize country dependent data here rather then in initialize()

    }

}

在中設(shè)置上下文SampleController:


Context currentContext = new Context();


@Override

public void initialize(URL url, ResourceBundle rb) {

    currentContext.currentCountry().setCountry("Belgium");

}


@FXML

private void handleButtonAction(ActionEvent event) throws IOException {

    URL url = getClass().getResource("Sub1.fxml");


    FXMLLoader fxmlloader = new FXMLLoader();

    fxmlloader.setLocation(url);

    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());


    pContent.getChildren().clear();

    pContent.getChildren().add((Node) fxmlloader.load(url.openStream()));

            // here we go

    ((Sub1Controller)fxmlloader.getController()).setContext(currentContext);

}


查看完整回答
反對 回復(fù) 2019-11-12
?
暮色呼如

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊

感謝謝爾蓋一百萬次,終于一切都說得通了。這是我第一次必須創(chuàng)建一個(gè)大型應(yīng)用程序,但是我仍在努力“構(gòu)建”它。我還想問另一個(gè)安全性問題,但是我不確定這是否是問這個(gè)問題的正確網(wǎng)站(它與代碼無關(guān))

查看完整回答
反對 回復(fù) 2019-11-12
  • 3 回答
  • 0 關(guān)注
  • 664 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號