[英]How to use same method in two different classes (JavaFX with Scenebuilder)?
I am currently experimenting with JavaFX and SceneBuilder in eclipse to create and design my own program. 我目前正在Eclipse中尝试JavaFX和SceneBuilder来创建和设计自己的程序。 In my first class "StartController" I am using a method called makeFadeIn .
在我的第一个类“ StartController”中,我使用了一个名为makeFadeIn的方法。 Basically, when I click a button another page loads up with a fade effect.
基本上,当我单击一个按钮时,另一页会加载淡入淡出效果。
This is the code from StartController.java (notice makeFadeIn): 这是来自StartController.java的代码(注意makeFadeIn):
public class StartController {
@FXML
private AnchorPane rootPane;
private void makeFadeIn() {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
fadeTransition.setNode(rootPane);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
@FXML
private void loadSecondPage(ActionEvent event) throws IOException {
AnchorPane startPage = FXMLLoader.load(getClass().getResource("SecondController.fxml"));
rootPane.getChildren().setAll(startPage);
makeFadeIn();
}
Next, my other class loads up called "SecondController.java". 接下来,我的另一个类加载为“ SecondController.java”。 In this class, I'm using the exact same method makeFadeIn (but I had to write it twice since it didn't let me run the program).
在这个类中,我使用的是完全相同的方法makeFadeIn (但是我不得不编写两次,因为它不允许我运行程序)。
This is the code from SecondController.java: 这是SecondController.java中的代码:
public class SecondController {
@FXML
private AnchorPane rootPane;
private void makeFadeIn() {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
fadeTransition.setNode(rootPane);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
@FXML
private void loadFirstPage(ActionEvent event) throws IOException {
AnchorPane startPage = FXMLLoader.load(getClass().getResource("StartController.fxml"));
rootPane.getChildren().setAll(startPage);
}
My question is: can I somehow call the makeFadeIn method from the first class so I don't have to write it in my second class? 我的问题是:我可以以某种方式从第一类调用makeFadeIn方法,这样就不必在第二类中编写它了吗? I guess I need to inherit it in some way but I'm not sure how.
我想我需要以某种方式继承它,但我不确定如何继承。 I tried declaring it public instead of private but that did not help much.
我尝试将其声明为公开而不是私有,但这并没有太大帮助。
You could move this functionality to a base class: 您可以将此功能移至基类:
public class BaseController {
@FXML
private AnchorPane rootPane;
protected AnchorPane getRootPage() {
return rootPane;
}
protected void makeFadeIn() {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
fadeTransition.setNode(rootPane);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
}
And then have the other controllers extend it: 然后让其他控制器对其进行扩展:
public class StartController extends BaseController {
@FXML
private void loadSecondPage(ActionEvent event) throws IOException {
AnchorPane startPage =
FXMLLoader.load(getClass().getResource("SecondController.fxml"));
getRootPane().getChildren().setAll(startPage);
makeFadeIn();
}
}
public class SecondController extends BaseController {
@FXML
private void loadFirstPage(ActionEvent event) throws IOException {
AnchorPane startPage =
FXMLLoader.load(getClass().getResource("StartController.fxml"));
getRootPane().getChildren().setAll(startPage);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.