【问题标题】:fxml: How to handle button action in the controller?fxml:如何处理控制器中的按钮动作?
【发布时间】:2019-12-17 08:06:55
【问题描述】:

我有一个最小的例子:控制器,主,带有一个按钮和两个文本区域的 fxml 文件,一个输入,另一个输出。按下按钮时,输入将打印在输出中。当我在 fxml (onAction="#printOutput") 中插入按钮的操作时,它工作正常。 我想知道如何(以及为什么不)我可以在控制器中执行相同的操作而无需处理 fxml。注意:我来自 Android,我避免在 xml 中实现相同的功能,因为调试非常困难,而且它不提供灵活性。文件如下:

控制器

public class FxFXMLController {
    @FXML private TextField inputText;
    @FXML private TextArea outputText;
    @FXML private Button okBtn;

    //Do I connect here an okBtn.setAction() with printOutput? if not, where?
    public FxFXMLController() {
        // Nothing
    }

    @FXML
    private void printOutput() {
        // insert this into outputText in fxml file: onAction="#printOutput"
        outputText.setText(inputText.getText());
    }
}

主要

public class FxFXMLExample3 extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        VBox root = (VBox) loader.load((getClass().getResource("FxFXMLExample3.fxml")));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("A FXML Example with a Controller");
        stage.show();
    }
}

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox fx:id="vbox" layoutX="10.0" layoutY="10.0" prefHeight="250.0" prefWidth="300.0" spacing="10" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.FxFXMLController">
    <style>
        -fx-padding: 10;
        -fx-border-style: solid inside;
        -fx-border-width: 2;
        -fx-border-insets: 5;
        -fx-border-radius: 5;
        -fx-border-color: blue;
    </style>

    <Label fx:id="inputLbl" alignment="CENTER_LEFT" cache="true" cacheHint="SCALE" prefHeight="30.0" prefWidth="200.0" text="Please insert Your Input here:" textAlignment="LEFT" />
    <TextField fx:id="inputText" prefWidth="100.0" />
    <Button fx:id="okBtn" alignment="CENTER_RIGHT" contentDisplay="CENTER" mnemonicParsing="false" text="OK" textAlignment="CENTER" />
    <Label fx:id="outputLbl" alignment="CENTER_LEFT" cache="true" cacheHint="SCALE" prefHeight="30.0" prefWidth="200.0" text="Your Input:" textAlignment="LEFT" />
    <TextArea fx:id="outputText" prefHeight="100.0" prefWidth="200.0" wrapText="true" />
</VBox>

【问题讨论】:

  • 您可以使用Initialize method。它的里面做okBtn.setOnaction(t-&gt;{outputText.setText(inputText.getText());});
  • 谢谢,明白了。哲学问题:在控制器和主程序中处理 GUI 元素之间有什么优缺点?
  • 什么是“主要”?如果那是应用程序类,那么处理 GUI 元素的缺点是您无法轻松访问它们;控制器让您可以直接访问所需的所有 GUI 元素。

标签: javafx fxml


【解决方案1】:

你可以使用初始化函数在你的按钮上注册一个新的事件监听器 然后你在那个听众身上做任何你想做的事。 这是代码:

public class FxFXMLController {
    @FXML private TextField inputText;
    @FXML private TextArea outputText;
    @FXML private Button okBtn;

    @FXML
    public void initialize() {
       okBtn.setOnAction(event -> {
        this.printOutput();
      });
    }

    public FxFXMLController() {
        // Nothing
    }

    @FXML
    private void printOutput() {
        // insert this into outputText in fxml file: onAction="#printOutput"
        outputText.setText(inputText.getText());
    }
}

【讨论】:

  • 我会删除 @FXML 方法上的 @FXML
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多