【发布时间】:2019-01-15 10:02:46
【问题描述】:
我正在尝试使用 JavaFX 使用按钮作为像素来制作 sprite editor。我可以在按下时更改每个按钮的颜色,但我正在尝试获取它,所以如果我单击并拖动我可以绘制多个像素。
我发现的问题是,单击并按住按钮后,当我将鼠标移到新按钮上以选择该按钮时,我无法选择新按钮。如果我单击并拖动重新输入该按钮,我可以得到“Paint Dragged Pixel”调试消息,但如果我在鼠标按下的情况下输入一个新像素,这就是我想要的。当鼠标进入任何按钮时,我也可以让像素按钮打印“输入的像素”,但当我单击并拖动到新像素时则不行。
我认为问题在于,当我单击一个像素时,我被锁定,无法通过将鼠标悬停在新像素上来选择新像素。有没有办法取消绑定这个选择,还是问题不同。
主要应用:
public class Main extends Application {
boolean mousePressed = false;
public boolean isMousePressed() {
return mousePressed;
}
@Override
public void start(Stage primaryStage) throws Exception{
BorderPane borderPane = new BorderPane();
primaryStage.setTitle("SpriteSheet");
Group root = new Group();
Scene scene = new Scene(borderPane, 500,200);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
GridPane gridPane = new GridPane();
borderPane.setCenter(root);
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++) {
PixelButton button = new PixelButton();
button.setParentMain(this);
button.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mousePressed = true;
System.out.println("mouseDown");
}
});
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mousePressed = false;
System.out.println("mouseUp");
}
});
gridPane.add(button, x, y);
}
}
root.getChildren().add(gridPane);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
PixelButton 的类。
public class PixelButton extends Button {
Main parentMain;
public void setParentMain(Main parent) {
parentMain = parent;
}
public PixelButton() {
this.setMinSize(10, 10);
this.setPrefSize(10, 10);
this.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
}
});
this.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("Entered Pixel");
if(parentMain.isMousePressed()){
System.out.println("Paint Dragged Pixel");
}
}
});
}
}
提前谢谢你。
【问题讨论】:
-
嗯..“选择”是什么意思?这不是按钮语义的一部分(也不是鼠标处理程序,faiw)无论如何,无论您尝试做什么,您在 main 中的每个按钮上注册的处理程序都将替换您在按钮本身中注册的处理程序(对于相同的事件类型) .要么决定你想要哪个,要么用 addEventHandler 替换一个注册。还要注意,一个释放被传递到接收到按下的节点
-
您好,感谢您的回复。通过选择我指的是它没有被调用。我相信这是仅在释放鼠标按钮时调用的 isarmed() 属性。我尝试使用 fire() 手动调用按钮,但到目前为止没有成功。我也尝试过解除()按钮。覆盖是正确的。 PixelButton 中的 setOnMousePressed() 被覆盖。我现在主要实现了它,但它没有解决按下和拖动问题。它是像素按钮的 OnMouseEnter(),我对它为什么不起作用感到困惑。如果鼠标进入并被按下。它应该在我的脑海中。
标签: button javafx mouseevent