【发布时间】:2016-07-02 08:28:17
【问题描述】:
我的问题是:我想从 .fxml 中更改一些内容,但无论我做什么,都没有任何改变。这只是一个简单的例子。
我浏览了整个互联网,但没有一个解决方案适合我。 这里我想通过在主类中调用相应的方法来更改标签的文本。
单击按钮时调用相同的方法(此处为setLabel()),使用控制器类中的事件处理程序,一切正常,但是当我尝试从另一个类修改某些内容时,没有任何作用。
主类:
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
Controller controller = new Controller();
Platform.runLater(()->controller.setLabel());
}
FXML 代码:
<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Label fx:id="label" text="This" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
控制器类: 包装样品;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller {
@FXML
private Label label=new Label();
public void setLabel(){
label.setText("Test");
}
}
【问题讨论】:
标签: java javafx controller label fxml