【发布时间】:2017-12-13 10:44:14
【问题描述】:
我一直在尝试显示所选饼图切片的百分比。我在谷歌上搜索过,每个人都在添加一个带有鼠标事件的新事件处理程序。每当我尝试实现代码时,都会出现以下错误:
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;
}
}
谢谢
编辑:添加整个控制器
【问题讨论】:
-
请包含完整的文件 - 最可能的解释是您导入了
MouseEvent的 AWT 或 Swing 版本而不是 JavaFX。 -
@sillyfly 我已经编辑了帖子
-
@sillyfly 我已经用 javafx 变体替换了导入,它的工作原理。感谢您的帮助。您可以将此作为答案发布,以便我接受吗?