简体   繁体   English

如何在另一个 class 中获取实例? 在 java

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

I am learning javaFX but I can't figure out how to get an instance that I created in another controller into my new Controller.我正在学习 javaFX 但我不知道如何将我在另一个 controller 创建的实例放入我的新 Controller 中。

Class of Auto 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;
    }

}

My controller of my main fxml我的主要 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));
    }


}

My controller of my second fxml我的第二个 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());
    }
}

Can somebody help me please?有人可以帮我吗?

I really appreciate it thanks.我真的很感激谢谢。

Add this to your controller of your main fxml将此添加到您的主要 fxml 的 controller

Public Controller (){ }公共 Controller (){ }

And this on new controller fxml Controller controller = new Controller ();这在新的 controller fxml Controller controller = new Z9BBF373797BF7CF7BA262C800236 () controller.maakNieuweAuto(); controller.maakNieuweAuto();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Java中的另一个类获取类的特定实例? - How to get specific instance of class from another class in Java? 如何从Java中的另一个类获取变量? - How can I get a variable from another class in Java? 如何从另一个类的静态方法获取抽象类的子类的实例? - How can I get an instance of a subclass of abstract class from a static method of another class? 如何通过课程获取实例? - How I can get a instance by a class? 如何在Java中使用一个方法在一个类中创建另一个实例? - How can I use a method in one class to create an instance of another in Java? 如何使用作为命令参数传输的 String 变量到 Java 中另一个类的实例? - How can I use the String variable transferred as the command parameter to a instance of another class in Java? 如何获取已经在另一个 class 中设置的实例值? - How can I get a value of instance which is already set in another class? 如何通过 Java 反射获得未命名的内部 class 的实例? - How can I get an instance of non-named inner class by Java reflection? 我如何从另一个班级收到实例? - How can I receive instance from another class? 如何在Java的同一包中获取由一个类中的一个函数生成的数据在另一个类中 - How can I get data generated by one function in one class in another class in same package in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM