javafx piechart无法添加eventhandler

时间:2017-12-13 10:44:14

标签: javafx pie-chart

我一直试图显示所选饼图切片的百分比。我在Google上搜索过,每个人都在添加一个带有鼠标事件的新事件处理程序。每当我尝试实现代码时,我都会收到以下错误:

method addEventHandler in class Node cannot be applied to given types;
  required: EventType<T>,EventHandler<? super T>
  found: int,<anonymous EventHandler<MouseEvent>>
  reason: cannot infer type-variable(s) T
    (argument mismatch; int cannot be converted to EventType<T>)
  where T is a type-variable:
    T extends Event declared in method <T>addEventHandler(EventType<T>,EventHandler<? super T>)

我做错了什么?我尝试使用错误搜索谷歌,但没有任何结果。

有问题的控制器:

package com.hva.fastenyourseatbelt;

import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.chart.PieChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import utilities.DatabaseUtils;
import static utilities.StatisticsUtils.giveAirports;

/**
 * FXML Controller class
 *
 * @author Mourad A
 */
public class FXMLStatistiekenController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private ComboBox<String> CBStatisticsCountry;

    @FXML
    private ComboBox<String> CBStatisticsAirport;

    @FXML
    private ComboBox<String> CBStatistiekenYear;

    @FXML
    private ComboBox<String> CBStatistiekenMonth;

    @FXML
    private PieChart pieChart;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        ObservableList list = DatabaseUtils.getType();

        pieChart.setData(list);
        pieChart.setTitle("Type bagage");

        final Label caption = new Label("");
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");

        for (final PieChart.Data data : pieChart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
                    new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent e) {
                    caption.setTranslateX(e.getSceneX());
                    caption.setTranslateY(e.getSceneY());
                    caption.setText(String.valueOf(data.getPieValue()) + "%");
                }
            });
        }
    }

    @FXML
    private String receiveCountries(ActionEvent event) {
        String country = CBStatisticsCountry.getValue();
        return country;
    }


}

由于

编辑:添加整个控制器

1 个答案:

答案 0 :(得分:0)

错误是由于错误地导入java.awt.event.MouseEvent而不是javafx.scene.input.MouseEvent。最大的提示是实际类型是int而不是EventType<T>,这意味着导入不匹配 - 较旧的API倾向于使用静态整数常量,而较新的API更喜欢枚举。