我正在使用JavaFX和Scene Builder制作应用程序。
我有两个控制器:Controller和FontController
我有Main类来启动程序并使用第一个fontroller(控制器)打开Stage
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("/card/card.fxml"));
Scene scene = new Scene(root, 1600, 600);
primaryStage.setScene(scene);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setMaximized(true);
primaryStage.setResizable(true);
primaryStage.getIcons().add(new Image("card/resources/logo-icon.png"));
primaryStage.show();
//adding resize and drag primary stage
ResizeHelper.addResizeListener(primaryStage);
//assign ALT+ENTER to maximize window
final KeyCombination kb = new KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (kb.match(event)) {
primaryStage.setMaximized(!primaryStage.isMaximized());
primaryStage.setResizable(true);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
控制器中有一个标签和一个按钮。当我单击按钮时,将调用一个方法,并出现带有第二个控制器的新窗口(FontController):
@FXML private Button btnFont;
@FXML private Label category1
@FXML
void changeFont(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("font.fxml"));
Parent rootFont = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Select Font");
stage.setScene(new Scene(rootFont));
stage.show();
} catch (Exception e) {
System.out.println("can't load new window");
}
}
FontCOntroller中有“确定”按钮和标签:
@FXML private Label fontLabel;
@FXML private Button btnFontOk;
请告诉我,当我单击Burtton“确定”以在Controller中进行标签时,如何发送和应用FontController中的标签文本?
答案 0 :(得分:1)
解决方案:
我在项目目录中创建了“上下文”类,以使所有控制器相互通信。您可以在其中添加任意数量的控制器。
这里看起来像:
package card;
public class Context {
private final static Context instance = new Context();
public static Context getInstance() {
return instance;
}
private Controller controller;
public void setController(Controller controller) {
this.controller=controller;
}
public Controller getController() {
return controller;
}
private FontController fontController;
public void setFontController(FontController fontController) {
this.fontController=fontController;
}
public FontController getFontController() {
return fontController;
}
}
控制器:
我为要更改的标签创建了吸气剂和吸气剂(ALT +在IDEA中插入)
public Label getCategory1() {
return category1;
}
public void setCategory1(Label category1) {
this.category1 = category1;
}
要通过Context类获取FontController变量和方法,我放置了代码行
//getting FontController through Context Class
FontController fontCont = Context.getInstance().getFontController();
我通过我的initialize方法在Context类中注册了Controller(我的类实现了Initializable)
@FXML
public void initialize(URL location, ResourceBundle resources) {
//register Controller in Context Class
Context.getInstance().setController(this);
}
FontController:
要获取控制器变量和方法,请放置以下代码:
//getting Controller variables and methods through Context class
Controller cont = Context.getInstance().getController();
我还通过初始化方法在Context类中注册了FontController:
@Override
public void initialize(URL location, ResourceBundle resources) {
//register FontController in Context Class
Context.getInstance().setFontController(this);
}
当我单击按钮时,将文本和文本颜色从此FontController中的标签发送到Controller中的标签的方法:
@FXML
void applyFont(ActionEvent event) {
cont.getCategory1().setText(fontLabel.getText());
cont.getCategory1().setTextFill(fontLabel.getTextFill());
}
*通过创建Context类,可以使控制器彼此通信,并在其中创建任意数量的控制器。控制器看到彼此的变量和方法