Fx开发教程--控制器篇----转载

 在JavaFX的UI开发中,FXController是个很重要的东西,主要是用于UI层和事件层分离。

 事实上,JavaFX使用FXML来开发UI界面,有多种形式来监听我们的事件,下面我们来细看。

 1.通过Controller Class来处理事件

 首先我们创建一个简单的界面,包含一个Button和一个Label。

 如下图:

 

  Label的fx:id设置为mLabel,Button的fx:id设置为mButton,同时将Button的onAction设置为onButtonClick。

  如下图所示:

  然后我们创建一个MainController类,写下如下代码:

 
  1. import javafx.fxml.FXML;

  2. import javafx.scene.control.Button;

  3. import javafx.event.ActionEvent;

  4. import javafx.scene.control.Label;

  5.  
  6. public class MainLayoutController {

  7. @FXML

  8. private Button mButton;

  9. @FXML

  10. private Label mLabel;

  11.  
  12. @FXML

  13. public void onButtonClick(ActionEvent event) {

  14. mLabel.setText("HelloWorld");

  15. }

  16. }


  记住,我们需要在FXML的最上层添加fx:controller = "" 指向自己的MainController类(带包名)。

  我们的Main类如下:

 
  1. import javafx.application.Application;

  2. import javafx.fxml.FXMLLoader;

  3. import javafx.stage.Stage;

  4. import javafx.scene.Parent;

  5. import javafx.scene.Scene;

  6.  
  7.  
  8. public class Main extends Application {

  9. @Override

  10. public void start(Stage primaryStage) {

  11. try {

  12. Parent parent = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));

  13. Scene scene = new Scene(parent,300,200);

  14. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

  15. primaryStage.setScene(scene);

  16. primaryStage.show();

  17. } catch(Exception e) {

  18. e.printStackTrace();

  19. }

  20. }

  21.  
  22. public static void main(String[] args) {

  23. launch(args);

  24. }

  25. }


  通过FXMLLoader加载FXML,并添加到Scene里面。

  运行效果如下:

  当我们点击按钮的时候,文本内容变成HelloWorld。

  这个就是我之前的文章中曾经讲过的事件方式。

  2.像Android一样处理事件

  接下来,我们来看看另外一种处理事件的方式。

  事实上,JavaFX提供类似于Android的一些方法,我们可以通过fx:id来查找指定的控件,并通过代码实现我们的事件。

  我们将上面的Main方法改动一下如下:

 
  1. import javafx.application.Application;

  2. import javafx.fxml.FXMLLoader;

  3. import javafx.stage.Stage;

  4. import javafx.scene.Parent;

  5. import javafx.scene.Scene;

  6. import javafx.scene.control.Button;

  7. import javafx.scene.control.Label;

  8.  
  9.  
  10. public class Main extends Application {

  11. @Override

  12. public void start(Stage primaryStage) {

  13. try {

  14. Parent parent = FXMLLoader.load(getClass().getResource("MainLayout.fxml"));

  15. Label label = (Label)parent.lookup("#mLabel");

  16. Button button = (Button)parent.lookup("#mButton");

  17. button.setOnAction(e ->{

  18. label.setText("HelloWorld JavaFX");

  19. });

  20. Scene scene = new Scene(parent,300,200);

  21. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

  22. primaryStage.setScene(scene);

  23. primaryStage.show();

  24. } catch(Exception e) {

  25. e.printStackTrace();

  26. }

  27. }

  28.  
  29. public static void main(String[] args) {

  30. launch(args);

  31. }

  32. }

  我们通过lookup根据fx:id来查找控件,并添加事件处理。

  运行效果如下:

  大家可以明显看见,我们通过lookup查找到控件后,添加的事件覆盖了FXController中的事件。

  这就是另外一种类似Android的查找控件-添加事件的模式,可以根据自己的需要酌情处理。

  另外在e(fx)clipse 1.1版本里面,已经可以像Nebeans一样,通过fxml自动生成FXController了,还是非常的方便的。

  本文章为个人原创,版权所有,转载请注明出处:http://blog.csdn.net/ml3947。另外我的个人博客:http://www.wjfxgame.com

猜你喜欢

转载自blog.csdn.net/qq_36505948/article/details/81534272