JavaFX 部分遮挡的窗口是否仍会在被另一个窗口遮挡的窗口部分中接收鼠标移动事件?这似乎是奇怪的行为。添加额外的窗口(同一应用程序或其他应用程序窗口中的 javaFX)会导致奇怪和不可预测的行为
我做错什么了吗?
有没有办法避免这种行为?
package obscuredmouseevents;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* @author michaelellis
*/
public class ObscuredMouseEvents extends Application {
/**
* After launching drag the mouse from left to right through the left edge
* of the light blue panel of Window 1 and see the mouse coordinates
* displayed in the text area at the bottom of Window 1. However, continue
* dragging into the light blue panel of Window 2 and see not only the mouse
* coordinates displayed in the text area at the bottom of Window 2, but
* also the mouse coordinates displayed in the text area at the bottom of
* Window 1.
* <p>
* Clicking in Window 2 stops mouse events being registered in Window 1,
* however moving the mouse back into the light blue panel of Window 1, and
* on into the light blue panel of Window 2 see the problem occur again.
*
* @param primaryStage
*/
@Override
public void start(Stage primaryStage) {
createNewWindow("Window 1", 100, 100).show();
createNewWindow("Window 2", 160, 120).show();
}
Stage createNewWindow(String title, int x, int y) {
BorderPane root = new BorderPane();
Rectangle r = new Rectangle(300, 200);
r.setFill(Color.ALICEBLUE);
root.setCenter(r);
final TextField tf = new TextField();
root.setBottom(tf);
r.setOnMouseMoved(e -> {
tf.setText(String.format("%d,%d", (int) e.getX(), (int) e.getY()));
e.consume();
});
Scene scene = new Scene(root, 300, 300);
Stage stage = new Stage();
stage.setX(x);
stage.setY(y);
stage.setTitle(title);
stage.setScene(scene);
return stage;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
OS-X 埃尔卡皮坦 10.11.6 (15G1421)
MacBook Pro(视网膜显示屏,15 英寸,2013 年初)
Java 版本“1.8.0_121”Java(TM) SE 运行时环境(构建 1.8.0_121-b13)
Java HotSpot(TM) 64 位服务器 VM(内部版本 25.121-b13,混合模式)
于 2017 年 12 月 22 日添加以进一步探索该问题 以下示例添加了鼠标进入/退出处理和第三个窗口。有一些非常奇怪和不稳定的行为。还可以尝试单击前窗口,以阻止将鼠标移动的事件传递到隐藏的窗口,并重新排序窗口的堆栈位置,这会显示有关将事件传递到隐藏的窗口的更不稳定的行为!
package obscuredmouseevents;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ObscuredMouseEvents extends Application {
@Override
public void start(Stage primaryStage) {
createNewWindow("Window 1", 100, 100).show();
createNewWindow("Window 2", 150, 120).show();
createNewWindow("Window 3", 200, 140).show();
}
Stage createNewWindow(String title, int x, int y) {
BorderPane root = new BorderPane();
Rectangle r = new Rectangle(280, 240);
r.setFill(Color.ALICEBLUE);
root.setCenter(r);
final CheckBox checkBox = new CheckBox("inside");
root.setTop(checkBox);
final TextField tf = new TextField();
root.setBottom(tf);
r.setOnMouseMoved(e -> {
tf.setText(String.format("%d,%d", (int) e.getX(), (int) e.getY()));
e.consume();
});
r.setOnMouseEntered(e -> {
checkBox.setSelected(true);
e.consume();
});
r.setOnMouseExited(e -> {
checkBox.setSelected(false);
e.consume();
});
Scene scene = new Scene(root, 300, 300);
Stage stage = new Stage();
stage.setX(x);
stage.setY(y);
stage.setTitle(title);
stage.setScene(scene);
return stage;
}
public static void main(String[] args) {
launch(args);
}
}