【问题标题】:How to pass a Variable through JavaFX Application to the Controller? [duplicate]如何通过 JavaFX 应用程序将变量传递给控制器​​? [复制]
【发布时间】:2014-08-16 01:38:33
【问题描述】:

我想问一下是否可以通过 JavaFX 类传递变量,什么将 Application 扩展到我的 JavaFx 控制器?我对 JavaFx 很陌生,可能只需要一点点。

目标是从 MyClass 向 MyController 传递一个 Id。

我的应用程序类:

public class MyClass extends Application {
    private String myVariable="Anything";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        URL location = getClass().getResource("MyGui.fxml");

        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        Parent root = FXMLLoader.load(location);
        Scene scene = new Scene(root);

        stage.setTitle(title);
        stage.setScene(scene);
        stage.show();
    }
}

我的控制器:

public class Controller extends Group implements Binding {

public void initialize(Map<String, Object> namespace, URL location, Resources resources) {

// HERE I'D LIKE TO GET MY VARIABLE LIKE
System.out.println(myVariable);
}

@Override
public List<Handler> getHandlerChain() {
    return null;
}

@Override
public void setHandlerChain(List<Handler> chain) {
}

@Override
public String getBindingID() {
    return null;
}
}

【问题讨论】:

标签: java variables controller javafx fxml


【解决方案1】:

首先,您必须在 MyClass 中添加 setter 和 getter(因为 var 是 private)并将其更改为静态:

private static String myVariable;
public String getMyVariable() {
    return myVariable;
}

public void setMyVariable(String myVariable) {
    this.myVariable = myVariable;
}

然后,由于 MyClass 是静态的,可以这样做:

System.out.println(MyClass.getMyVariable());

工作示例:

MyClass.java

import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MyClass extends Application {
    private static String myVariable;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        setMyVariable("Anything");
        URL location = getClass().getResource("MyGui.fxml");
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(location);
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        Parent root = FXMLLoader.load(location);
        Scene scene = new Scene(root);
        stage.setTitle("Hello Word");
        stage.setScene(scene);
        stage.show();
    }

    public static String getMyVariable() {
        return myVariable;
    }

    public static void setMyVariable(String myVariable) {
        MyClass.myVariable = myVariable;
    }

}

MyController.java

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;


public class MyController implements Initializable{
    @FXML Label labelVar;
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        labelVar.setText(labelVar.getText() + MyClass.getMyVariable());

    }

}

MyGui.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="110.0" prefWidth="305.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
    <children>
        <Label fx:id="labelVar" layoutX="24.0" layoutY="32.0" prefHeight="17.0" prefWidth="129.0" text="MyVariable = " />
    </children>
</Pane>

【讨论】:

  • 这不起作用,因为 getter 和 setter 不是静态的,因此无法从类名中引用方法。
  • @CAGGonzo,只要你让他们static,它就会。感谢您指出我在原始答案中错过了它。
  • 非常感谢,我自己做的,但方法相同。
  • 不需要使用静态变量。查看this帖子的详细答案和this帖子的非常详细的答案
猜你喜欢
  • 2020-02-26
  • 2022-07-28
  • 2017-12-27
  • 2017-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多