【问题标题】:JavaFX button unselectJavaFX 按钮取消选择
【发布时间】: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


【解决方案1】:

好的,所以我一直在考虑这个问题,最后想出了一个解决方案从这开始不是您正在寻找的确切功能,但基本上我可以尽可能接近我在鼠标按下时触发一个事件,释放鼠标点击,只要该事件不是来自矩形,那么不要翻转绘画布尔绘画,所以你基本上点击进入“绘画模式”,然后再次点击退出着色砖

public class Main extends Application {    

    private boolean 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++) {
                Rectangle rectangle = new Rectangle(10, 10);
                rectangle.setOnMousePressed(event -> {
                    mousePressed = true;
                    System.out.println("mouseDown");
                    rectangle.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED,
                            rectangle.getLayoutX(), rectangle.getLayoutY(), rectangle.getLayoutX(), rectangle.getLayoutY(),
                            MouseButton.PRIMARY, 1,
                            false, false, false, false,
                            false, false, false, false,
                            false, false, null));
                });

                rectangle.setOnMouseReleased(event -> {
                    System.out.println(event.getSource());
                    if(!event.getSource().toString().equals("Rectangle[x=0.0, y=0.0, width=10.0, height=10.0, fill=0x000000ff]")) {
                        mousePressed = false;
                        System.out.println("mouseUp");
                    }
                });

                rectangle.setOnMouseMoved(event -> {
                    if(mousePressed) {
                        rectangle.setFill(Color.BLUE);
                    }
                });

                gridPane.add(rectangle, x, y);
            }
        }
        root.getChildren().add(gridPane);
        primaryStage.show();
    }
    public static void main(String[] args) { launch(args); }
}

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2020-03-26
    • 1970-01-01
    • 2011-07-17
    • 2021-09-01
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    相关资源
    最近更新 更多