我对Java很陌生。我想使用一个类来更新按钮的文本,以便在调用该方法时相应地进行更新。我试图实现其他相关文章中的一些代码,但无法弄清楚。首先-您是否必须更新整个场景以更新按钮,还是像在更新DOM的部分那样起作用?
目标是在场景加载时更新按钮的文本。 FXML文件:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="project.Controller">
<Button fx:id="button" text="STOP"></Button>
</AnchorPane>
主要Java文件:
public class project extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(project.class.getResource("index.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root,1200,750);
primaryStage.setScene(scene);
primaryStage.show();
Controller editButton = new Controller();
editButton.editButtonText("blabla selected");
} catch (Exception e){
System.out.println(e);
}
}
public static void main(String[] args) {
launch(args);
}
}
类文件:
public class Controller implements Initializable
{
//FXML
@FXML public Button button;
@FXML
public void editButtonText(String text){
// button = new Button();
button.setText(text);
}
@Override
public void initialize(URL url,ResourceBundle r){
}
}