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 El Capitan 10.11.6(15G1421)
MacBook Pro(Retina,15英寸,2013年初)
java version“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);
}
}