javafx fxml获取识别不到controller里的方法

发布于 2022-09-05 09:06:45 字数 1404 浏览 11 评论 0

fxml

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

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


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="266.0" prefWidth="332.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">

   <children>
      <Button fx:id="myButton" layoutX="14.0" layoutY="53.0" mnemonicParsing="false"
              onAction="#showtime" text="显示时间"  />
      <TextField fx:id="myTextField" layoutX="86.0" layoutY="53.0" />
   </children>
</AnchorPane>

controller

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Controller {
    @FXML
    private Button myButton;

    @FXML
    private TextField myTextField;

    @FXML
    public void showtime(ActionEvent event) {
        System.out.println("button clicked");

        Date now = new Date();

        DateFormat df = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
        String dateTimeString = df.format(now);

        myTextField.setText(dateTimeString);
    }


}

button中的onAction="#showtime"一直显示错误,求解。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2022-09-12 09:06:46

ActionEvent event 要导入import javafx.event.ActionEvent,不是import java.awt.event.ActionEvent;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文