【问题标题】:how to run a method in JavaFX upon the opening of a new scene如何在打开新场景时在 JavaFX 中运行方法
【发布时间】:2017-03-22 03:59:46
【问题描述】:

我正在尝试根据用户在前一个场景中选择的按钮在某个场景中显示不同的内容。我曾尝试使用public static void main(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(){


    }
}

【问题讨论】:

  • 如果GrammarTestController是弹出场景的控制器,可以在控制器的initialize()方法中调用contentSelect(),因为该方法是由FXMLLoader自动调用的。跨度>
  • 嗨!感谢您的回答!但它仍然无法正常工作。
  • private void initialize(){ System.out.println("plz yes"); }
  • ^ 我将它添加到我的代码中间,但是当我打开 GrammarTestController 的视图时没有打印任何内容。我做错了吗?
  • 它还说:“来自 GrammarTestController 类型的方法 initialize() 永远不会在本地使用:”

标签: java javafx scene


【解决方案1】:

实现Initializable:

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(){


    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多