[英]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.