为什么FXML控制器不起作用

时间:2017-12-17 11:51:26

标签: java model-view-controller javafx

以下是我的代码:

public class MyController implements Initializable {
@FXML
private Button date_button;
@FXML
private TextField date_field;
@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

}

@FXML
public void showDateTime(ActionEvent event) {
    System.out.println("Button Clicked");
    Date now=new Date();
    DateFormat df=new SimpleDateFormat("yy-hh-dd hh:mm:ss");
    String dateString=df.format(now);
    System.out.println(dateString);
    date_field.setText(dateString);
}

}

和之后是控制台消息:

显示java.lang.NullPointerException     在application.MyController.showDateTime(MyController.java:33)

我的fxml文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="307.0" prefWidth="452.0" xmlns:fx="http://javafx.com/fxml/1" 
    xmlns="http://javafx.com/javafx/8"
    fx:controller="application.MyController">
   <children>
      <Button id="date_button" layoutX="28.0" layoutY="25.0" mnemonicParsing="false" 
      prefHeight="30.0" prefWidth="104.0" text="Show Date Time" onAction="#showDateTime"/>
      <TextField id="date_field" layoutX="28.0" layoutY="74.0" />
   </children>
</AnchorPane>

0 个答案:

没有答案