简体   繁体   English

Java FX fxml on Action

[英]Java FX fxml on Action

I want to add a method to a button which is defined in my Controller class我想向在我的Controller类中定义的按钮添加一个方法

in the console is only an error which tells me that it couldn't find the method在控制台中只是一个错误,告诉我它找不到方法

here is the code这是代码

sample.fxml示例文件

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

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Controller">
    <children>
        <Button layoutX="126" layoutY="90" text="lololol" onAction="#test"  fx:id="button" />
    </children>
</AnchorPane>

and the Controller.java和 Controller.java

package sample;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;

import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable
{
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {
    }
    @FXML
    private void test(ActionEvent event)
    {
        System.out.println("lollolol");
    }
}

Replace:代替:

import java.awt.event.ActionEvent;

with:和:

import javafx.event.ActionEvent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM