如何在打开新场景时在JavaFX中运行方法

如何在打开新场景时在JavaFX中运行方法,java,javafx,scene,Java,Javafx,Scene,我试图根据用户在前一个场景中选择的按钮在某个场景中显示不同的内容。我曾尝试使用公共静态voidmain(String[]args)和计时器来实现这一点,但我就是做不到 如何让contentSelect()在场景打开时运行? 我知道这应该很简单,但我不能让它为我的生活工作 package application; import java.time.Duration; import javafx.animation.KeyFrame; import javafx.animation.Timeli

我试图根据用户在前一个场景中选择的按钮在某个场景中显示不同的内容。我曾尝试使用
公共静态voidmain(String[]args)
和计时器来实现这一点,但我就是做不到

如何让contentSelect()在场景打开时运行? 我知道这应该很简单,但我不能让它为我的生活工作

package application;

import java.time.Duration;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;

public class GrammarTestController {

    private static int picSelect=0;
    @FXML
    private Label title;
    @FXML
    private Label info;
    @FXML
    private ImageView image;

    //Will decide which type of content to display
    private void contentSelect(){


    }
}

实现可初始化的

public class GrammarTestController implements Initializable{
    private static int picSelect=0;
    @FXML
    private Label title;
    @FXML
    private Label info;
    @FXML
    private ImageView image;

    //This method is called upon fxml load
    public void initialize(URL location, ResourceBundle resources) {
        contentSelect();
    }

    //Will decide which type of content to display
    private void contentSelect(){


    }
}

实现可初始化的

public class GrammarTestController implements Initializable{
    private static int picSelect=0;
    @FXML
    private Label title;
    @FXML
    private Label info;
    @FXML
    private ImageView image;

    //This method is called upon fxml load
    public void initialize(URL location, ResourceBundle resources) {
        contentSelect();
    }

    //Will decide which type of content to display
    private void contentSelect(){


    }
}

如果
GrammarTestController
是弹出场景的控制器,您可以在控制器的
initialize()
方法中调用
contentSelect()
,因为该方法由
fxmloader
自动调用。您好!谢谢你的回答!但是它仍然不起作用。private void initialize(){System.out.println(“plz yes”);}^我在代码中间添加了它,但在打开GrammarTestController的视图时没有打印任何内容。我做错了什么吗?它还说:“GrammarTestController类型中的initialize()方法从未在本地使用过:”如果
GrammarTestController
是弹出场景的控制器,您可以在控制器的
initialize()
方法中调用
contentSelect()
,因为该方法由
fxmloader
自动调用。您好!谢谢你的回答!但是它仍然不起作用。private void initialize(){System.out.println(“plz yes”);}^我在代码中间添加了它,但在打开GrammarTestController的视图时没有打印任何内容。我做错什么了吗?它还说:“GrammarTestController类型中的initialize()方法从未在本地使用过:”