我的问题是:我想从.fxml中更改某些内容,但是无论我做什么,都没有任何改变。这只是一个简单的例子。
我浏览了整个互联网,但没有一种解决方案对我有用。在这里,我想通过从主类调用相应的方法来更改标签的文本。
setLabel()
单击Button时,在控制器类中使用事件处理程序调用相同的方法(在此处),一切正常,但是当我尝试从另一个类中修改某些内容时,一切正常。
主班:
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");
}
}
这很简单...您可以通过以下方式更改Main类中的标签文本:在Controller类中为所需标签添加getter方法,然后使用控制器对象(loader.getController() )并更新其文本。或使用Main中的controller对象在Controller类内调用setter方法。正如DVagra所说,使用loader.getController()获取控制器对象。(其中loader是FXMLoader的对象)。
而且,您不需要Platform.runLater()来更新gui控件。由于您已经在FX线程上运行。
无论如何,这就是您所需要的。
主班
package sample;
import javafx.application.Application;
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{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("sample.fxml"));
Parent root = loader.load();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
Controller controller = loader.getController();
controller.setLabel("Test");
// Or
// controller.getLabel().setText("Test");
}
public static void main(String[] args) {
launch(args);
}
}
控制器类
package sample;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable{
@FXML
Label label;
//setter
public void setLabel(String labelText){
label.setText(labelText);
}
//getter for label
public Label getLabel() {
return label;
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane xmlns="http://javafx.com/javafx/8.0.60"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<center>
<Label fx:id="label" text="This" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句