我有一个Pane,在这个窗格中有另一个带有鼠标监听器的小窗格。即使按下鼠标,我想检测输入的鼠标。怎么样?? ...较小的窗格确实检测到鼠标输入但是如果鼠标在父窗格的其他位置被按下然后进入较小的窗格则不会。
这里我已经在代码中写了问题,虽然我的项目有问题,问题是一样的,所以我可以使用solusion。但这是问题所在!
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class MouseTest extends Application {
public void start(Stage primarysStage) throws Exception {
primarysStage.setTitle("Problem");
Pane wrapper = new FlowPane();
wrapper.setPrefSize(400, 400);
wrapper.setStyle("-fx-background-color: #FFFFFF");
Scene scene = new Scene(wrapper, 400, 400);
primarysStage.setScene(scene);
Pane innerPane = new Pane();
innerPane.setPrefSize(200, 200);
innerPane.setStyle("-fx-background-color: green");
EventHandler<MouseEvent> mouseEntered = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
innerPane.setStyle("-fx-background-color: black");
}
};
EventHandler<MouseEvent> mouseExited = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
innerPane.setStyle("-fx-background-color:green");
}
};
innerPane.setOnMouseEntered(mouseEntered);
innerPane.setOnMouseExited(mouseExited);
wrapper.getChildren().add(innerPane);
Label lb = new Label("\n\n\n\t\t\t\t+\n\nPress mouse here and enter the green pane:\n it does not detect!"
+ " i want it to detect mouse entered \nregardless of mouse pressed or not! HOW?");
wrapper.getChildren().add(lb);
primarysStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:3)
模仿这句话不要给他们带鱼,而是教他们如何钓鱼,让我给你看一招。
使用
scene.addEventFilter(MouseEvent.ANY, e -> System.out.println( e));
检测所有鼠标事件并在控制台中显示它们。有了这个,你会明白哪个事件会被捕获,它将帮助你解决有关鼠标事件的未来问题。或者,如果您使用Event.ANY
,则为任何事件。
答案 1 :(得分:-1)
我的解决方案是:
line.setOnMouseEntered(linehover);
line.setOnMouseExited(linehover);
为两个操作添加一个 EventHandler :
private EventHandler<MouseEvent> linehover = new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
// TODO Auto-generated method stub
if(event.getEventType() == MouseEvent.MOUSE_EXITED){
System.out.println("Exited");
}else if(event.getEventType() == MouseEvent.MOUSE_ENTERED){
System.out.println("Entered");
}
}
};