gpt4 book ai didi

event-handling - JavaFX 2 XYChart.Series 和 setOnMouseEntered

转载 作者:行者123 更新时间:2023-12-04 04:29:14 25 4
gpt4 key购买 nike

是否可以设置 XYChart.Series 的实例以作用于 setOnMouseEntered?在我看来,使其工作的一个先决条件是实现 EventTarget 接口(interface)。至于 JavaFX XYChart.Series,我想在光标触摸黄线时突出显示以下数据系列(XYChart.Series 的实例):
http://docs.oracle.com/javafx/2.0/charts/img/line-series.png
更准确地说,我想执行以下操作,但例如 XYChart.Series 而不是 Button:

public class Temp {
/*code removed*/
Button btn = new Button("Button to touch");
btn.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
System.out.println("Cursor just touched the button!");
}
});
/*code removed*/
}

最佳答案

Lookup适当的节点并向它们添加您想要的事件处理程序。

这是一个例子。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

public class LineChartSample extends Application {
@Override public void start(Stage stage) {
//create the chart
final NumberAxis xAxis = new NumberAxis();
final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Number of Month");
final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Stock Monitoring, 2010");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
series.getData().addAll(new XYChart.Data(1, 23),new XYChart.Data(2, 14),new XYChart.Data(3, 15),new XYChart.Data(4, 24),new XYChart.Data(5, 34),new XYChart.Data(6, 36),new XYChart.Data(7, 22),new XYChart.Data(8, 45),new XYChart.Data(9, 43),new XYChart.Data(10, 17),new XYChart.Data(11, 29),new XYChart.Data(12, 25));
lineChart.getData().add(series);

// show the scene.
Scene scene = new Scene(lineChart, 800, 600);
stage.setScene(scene);
stage.show();

// make the first series in the chart glow when you mouse over it.
Node n = lineChart.lookup(".chart-series-line.series0");
if (n != null && n instanceof Path) {
final Path path = (Path) n;
final Glow glow = new Glow(.8);
path.setEffect(null);
path.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
path.setEffect(glow);
}
});
path.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
path.setEffect(null);
}
});
}
}
public static void main(String[] args) { launch(args); }
}

关于event-handling - JavaFX 2 XYChart.Series 和 setOnMouseEntered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10212147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com