java fxml教程,如何将数据从Java传递到FXML?

In the Controller(or maybe the Main or another file, I'm not sure how all this works), we have the following:

String foo = "Foo.";

And in Scene Bilder-generated FXML, something like this:

How do I make the value of foo appear as the text on the button? And where should I store it, the controller or somewhere else? I'm still perplexed about how do the Main file, the controller(s), and FXML files exactly come together.

解决方案

You give the controller access to the UI elements defined in the FXML by injection. Specifically, in the FXML, give the UI element a fx:id attribute:

Now in your controller, define an @FXML-annotated field with a name that matches the fx:id attribute value:

public class Controller {

@FXML

private Button someButton ;

}

Now you can configure the button with whatever logic you need:

public class Controller {

@FXML

private Button someButton ;

public void initialize() {

String foo = "foo" ;

someButton.setText(foo);

}

}

To answer the "how does all this fit together" part of your question, consider the FXML and controller as a pair. The FXML defines the layout, while the controller defines the logic (handling user input, etc). The controller has access to the elements of the UI defined in the FXML file using the mechanism described above.

When an FXMLLoader loads the FXML file, in the default setup, the FXMLLoader creates an instance of your controller class, injects the @FXML-annotated fields into the controller instance, and calls the controller instance's initialize() method.

The Application subclass exists just as the starting point for your application. It will typically just load an FXML file, put the root of the FXML in a Scene and display the Scene in a primary stage. If you have a more complex application, you might also start up some services and background threads here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JavaFX和echarts绘制折线图,需要遵循以下步骤: 1. 首先在项目中引入echarts库。 2. 在FXML文件中添加一个WebView组件用于显示echarts图表。 3. 在Java代码中使用echarts库绘制折线图,并将图表数据传递FXML文件中的WebView组件进行显示。 下面是一个简单的示例代码: FXML文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.*?> <?import javafx.scene.web.*?> <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> <WebView fx:id="webView" prefHeight="400.0" prefWidth="600.0" /> </AnchorPane> ``` Java代码: ```java package sample; import javafx.fxml.FXML; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import netscape.javascript.JSObject; public class Controller { @FXML private WebView webView; public void initialize() { WebEngine webEngine = webView.getEngine(); webEngine.load(getClass().getResource("index.html").toExternalForm()); // 加载echarts网页模板 // 绘制折线图 JSObject window = (JSObject) webEngine.executeScript("window"); window.setMember("data", new int[]{10, 20, 30, 40, 50}); webEngine.executeScript("drawLineChart(data)"); } } ``` index.html文件: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Line Chart</title> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.2.1/echarts.min.js"></script> </head> <body> <div id="chart" style="width: 100%; height: 100%"></div> <script> function drawLineChart(data) { var chart = echarts.init(document.getElementById('chart')); var option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] }, yAxis: { type: 'value' }, series: [{ data: data, type: 'line' }] }; chart.setOption(option); } </script> </body> </html> ``` 在这个示例中,我们使用了echarts库绘制了一个折线图,并将图表数据传递给了FXML文件中的WebView组件进行显示。其中,index.html是一个echarts网页模板,用于显示图表;Controller类中的initialize方法用于绘制折线图并将数据传递给WebView组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值