JavaFX之Controll用法

本文介绍了JavaFX中FXController在UI和事件处理中的应用。通过创建FXML界面和MainController类,展示了如何使用Controller Class处理事件,以及如何像Android那样通过lookup方法查找控件并添加事件。在e(fx)clipse新版本中,FXController的生成更加便捷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

 1.通过Controller Class来处理事件

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

 如下图:

 

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

  如下图所示:

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

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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类如下:

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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方法改动一下如下:

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  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.youkuaiyun.com/ml3947

使用 JavaFXTableView 控件,你可以按照以下步骤进行操作: 1. 导入必要的 JavaFX 类: ```java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.stage.Stage; ``` 2. 创建 TableView 和相关的 TableColumn 对象: ```java TableView<YourDataClass> tableView = new TableView<>(); TableColumn<YourDataClass, String> column1 = new TableColumn<>("Column 1"); TableColumn<YourDataClass, Integer> column2 = new TableColumn<>("Column 2"); // ... ``` 3. 设置 TableView 的数据源(items): ```java ObservableList<YourDataClass> dataList = FXCollections.observableArrayList(); // 添加数据到 dataList tableView.setItems(dataList); ``` 4. 设置 TableColumn 的单元格值提供器(CellValueFactory): ```java column1.setCellValueFactory(new PropertyValueFactory<>("property1")); column2.setCellValueFactory(new PropertyValueFactory<>("property2")); // ... ``` 其中,"property1" 和 "property2" 是 YourDataClass 中的属性名称,用于获取对应列的数据。 5. 可选:设置 TableColumn 的单元格工厂(CellFactory)来自定义单元格的显示方式。 6. 将 TableColumn 添加到 TableView 中: ```java tableView.getColumns().add(column1); tableView.getColumns().add(column2); // ... ``` 7. 创建 Scene 并将 TableView 放入其中: ```java Scene scene = new Scene(tableView); ``` 8. 创建 Stage 并设置 Scene: ```java Stage stage = new Stage(); stage.setScene(scene); stage.show(); ``` 通过以上步骤,你就可以创建一个基本的 TableView,并在其中显示和操作表格数据了。你可以根据自己的需求,进一步设置 TableView 的其他属性和方法,以满足更复杂的功能要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值