javaFX-如何从另一个控制器使用控制器的函数或对象
我见过很多类似于我的问题,但我没有弄清楚如何解决我的问题,所以我的代码如下: 我有一个简单的主线: MainClassFXGui.javajavaFX-如何从另一个控制器使用控制器的函数或对象,javafx,scene,stage,Javafx,Scene,Stage,我见过很多类似于我的问题,但我没有弄清楚如何解决我的问题,所以我的代码如下: 我有一个简单的主线: MainClassFXGui.java public class MainClassFXGui extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource(
public class MainClassFXGui extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MyProgramMainGuiTab.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Assicurazione");
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
public class MainClassFXGui extends Application {
static private Scene scene;
static private Stage stage;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MyProgramMainGuiTab.fxml"));
this.scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Assicurazione");
stage.show();
this.stage = stage;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public static Scene getScene(){
return AssicurazioneFXGui.scene;
}
public static Stage getStage(){
return AssicurazioneFXGui.stage;
}
}
和三个fxml文件,其中一个包含另外两个
<fx:include source="MyTab1.fxml" />
MyTab1Controller.java
....
@FXML
private Label LeftSt;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
LeftSt.setText("");
}
public void setLeftSt(String st){
LeftSt.setText(st);
}
...
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
setLeftSt("Can I change the label of the MyProgramMainGuiTabController?");
}
private MyProgramMainGuiTabController controller;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
controller.setLeftSt("Can I change the label of the MyProgramMainGuiTabController?");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("MyProgramMainGuiTab.fxml"));
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
controller = (MyProgramMainGuiTabController)fxmlLoader.getController();
}
我试着这样做:
MyTab1Controller.java
....
@FXML
private Label LeftSt;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
LeftSt.setText("");
}
public void setLeftSt(String st){
LeftSt.setText(st);
}
...
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
setLeftSt("Can I change the label of the MyProgramMainGuiTabController?");
}
private MyProgramMainGuiTabController controller;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
controller.setLeftSt("Can I change the label of the MyProgramMainGuiTabController?");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("MyProgramMainGuiTab.fxml"));
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
controller = (MyProgramMainGuiTabController)fxmlLoader.getController();
}
但是我有一个空指针异常,或者如果我移动handleButtonAction函数中对象“controller”实例化的代码,我没有任何错误,但是标签没有改变
也许我可以在MainClassFXGui.java中创建一个静态场景和一个静态阶段?
但是我不知道怎么用它们
MainClassFXGui.java
public class MainClassFXGui extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MyProgramMainGuiTab.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Assicurazione");
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
public class MainClassFXGui extends Application {
static private Scene scene;
static private Stage stage;
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MyProgramMainGuiTab.fxml"));
this.scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Assicurazione");
stage.show();
this.stage = stage;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public static Scene getScene(){
return AssicurazioneFXGui.scene;
}
public static Stage getStage(){
return AssicurazioneFXGui.stage;
}
}
我可以用getScene()或getStage()做些什么吗?有什么想法或建议吗
多亏了大家我现在已经用这种方式解决了问题。如果有人有更好的解决方案,请写信。还因为如果我需要执行一个函数,这并不能解决问题。。谢谢 MyProgramMainGuiTabController.fxml
...
@FXML
private Label LeftSt;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
LeftSt.setText("");
}
...
@FXML
private Button myObject;
private Scene scene;
private Label lblDataLeft;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
setLeftSt("this works!");
}
public void setLeftSt(String st){
if(this.scene == null)
this.scene = myObject.getScene();
if(this.lblDataLeft==null)
this.lblDataLeft = (Label) scene.lookup("#LeftSt");
if (this.lblDataLeft!=null)
this.lblDataLeft.setText(st);
}
MyTab1Controller.fxml
...
@FXML
private Label LeftSt;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
LeftSt.setText("");
}
...
@FXML
private Button myObject;
private Scene scene;
private Label lblDataLeft;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
setLeftSt("this works!");
}
public void setLeftSt(String st){
if(this.scene == null)
this.scene = myObject.getScene();
if(this.lblDataLeft==null)
this.lblDataLeft = (Label) scene.lookup("#LeftSt");
if (this.lblDataLeft!=null)
this.lblDataLeft.setText(st);
}
在
MyTab1Controller
中定义StringProperty
,方法如下:
public class MyTab1Controller {
private final StringProperty leftSt = new SimpleStringProperty();
public StringProperty leftStProperty() {
return leftSt ;
}
public final String getLeftSt() {
return leftStProperty().get();
}
public final void setLeftSt(String leftSt) {
leftStProperty().set(leftSt);
}
// ...
@FXML
public void handleButtonAction() {
setLeftSt(...);
}
// ...
}
现在将fx:id
属性指定给fx:include
标记:
<Tab closable="false" text="MyTab1">
<content>
<fx:include fx:id="myTab1" source="MyTab1.fxml" />
</content>
</Tab>
然后,您可以观察属性,并在其更改时更新标签:
public class MyProgramMainGuiTabController {
@FXML
private MyTab1Controller myTab1Controller ;
@FXML
private Label leftSt;
public void initialize() {
leftSt.setText("");
myTab1Controller.leftStProperty().addListener((obs, oldValue, newValue) ->
leftSt.setText(newValue));
}
}
注意,这里的规则是控制器的字段名是
fx:id
属性的值,并附加单词“controller:fx:id
中的myTab1
解析为注入控制器的字段的myTab1Controller
。有关更多信息,请参阅。你能看看这个问题和我的答案是否已经有用了吗?:-谢谢-RainerHi Rainer,是的,我已经看过这个问题了,但没有帮到我:当我试图使用控制器时,如果我将代码插入到“Inalizalize”中,我会出现nullpointerexception,或者,如果我把代码放在函数中,它什么都没有。(也许我误解了?)你能说出你想要的情景吗?是否通过单击按钮更改标签文本?在何处加载MyTab1Controller.fxml?Hi Calips。是的,单击按钮或出现错误时必须更改标签。抱歉,我犯了一个错误,实际上MyTab1Controller.fxml是MyTab1Controller.java:主目录中加载的MyTab1Controller.fxml的控制器。。