簡體   English   中英

如何在另一個 class 中獲取實例? 在 java

[英]How can I get an instance in another class? in java

我正在學習 javaFX 但我不知道如何將我在另一個 controller 創建的實例放入我的新 Controller 中。

Class 汽車

package com.example.test1;

public class Auto {
    private static String merk;
    private static double prijs;

    public Auto(String merk, double prijs) {
        this.merk = merk;
        this.prijs = prijs;
    }

    public String getMerk() {
        return merk;
    }

    public double getPrijs() {
        return prijs;
    }

}

我的主要 fxml 的 controller


import java.io.IOException;

public class Controller {

    public Label random;
    public Button btn1;
    public static Auto auto;

    public void maakNieuweAuto() {
        auto = new Auto("bmw", 900);
    }

    public void button() throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("Auto.fxml"));
        Stage window = (Stage) btn1.getScene().getWindow();
        window.setScene(new Scene(root, 750, 500));
    }


}

我的第二個 fxml 的 controller

package com.example.test1;

import javafx.scene.text.Text;

import static com.example.test1.Controller.auto;

public class AutoController {

    public Text Merk;
    public Text Prijs;

    public AutoController() {
            Merk.setText(auto.getMerk());
            Prijs.setText("" + auto.getPrijs());
    }
}

有人可以幫我嗎?

我真的很感激謝謝。

將此添加到您的主要 fxml 的 controller

公共 Controller (){ }

這在新的 controller fxml Controller controller = new Z9BBF373797BF7CF7BA262C800236 () controller.maakNieuweAuto();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM