在JavaFX中为草图程序创建可拖动的选择框
我正在尝试为JavaFX中的草图程序创建一个可拖动的选择框,如下所示: 我只是不知道怎么做。我最初想这样做:按下鼠标时捕获鼠标坐标,在拖动结束时再次执行此操作,然后计算高度和宽度,并使用这些属性制作一个带有黑色边框的透明按钮 但是,后来我意识到,当我这样做时,在缩放平面时,不可能看到按钮,除非你绘制并删除了很多按钮在JavaFX中为草图程序创建可拖动的选择框,javafx,Javafx,我正在尝试为JavaFX中的草图程序创建一个可拖动的选择框,如下所示: 我只是不知道怎么做。我最初想这样做:按下鼠标时捕获鼠标坐标,在拖动结束时再次执行此操作,然后计算高度和宽度,并使用这些属性制作一个带有黑色边框的透明按钮 但是,后来我意识到,当我这样做时,在缩放平面时,不可能看到按钮,除非你绘制并删除了很多按钮 所以,我想知道是否有更好的方法来做类似的事情,或者我的推理是否正确?谢谢我会用一个矩形代替按钮。只需按照您描述的操作,但在鼠标拖动时更新矩形的大小(和位置),而不是仅在释放鼠标时添加
所以,我想知道是否有更好的方法来做类似的事情,或者我的推理是否正确?谢谢我会用一个
矩形
代替按钮
。只需按照您描述的操作,但在鼠标拖动时更新矩形的大小(和位置),而不是仅在释放鼠标时添加矩形
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class SelectionRectangle extends Application {
private double mouseDownX ;
private double mouseDownY ;
@Override
public void start(Stage primaryStage) {
Rectangle selectionRectangle = new Rectangle();
selectionRectangle.setStroke(Color.BLACK);
selectionRectangle.setFill(Color.TRANSPARENT);
selectionRectangle.getStrokeDashArray().addAll(5.0, 5.0);
Pane pane = new Pane();
pane.setMinSize(600, 600);
pane.getChildren().add(selectionRectangle);
pane.setOnMousePressed(e -> {
mouseDownX = e.getX();
mouseDownY = e.getY();
selectionRectangle.setX(mouseDownX);
selectionRectangle.setY(mouseDownY);
selectionRectangle.setWidth(0);
selectionRectangle.setHeight(0);
});
pane.setOnMouseDragged(e -> {
selectionRectangle.setX(Math.min(e.getX(), mouseDownX));
selectionRectangle.setWidth(Math.abs(e.getX() - mouseDownX));
selectionRectangle.setY(Math.min(e.getY(), mouseDownY));
selectionRectangle.setHeight(Math.abs(e.getY() - mouseDownY));
});
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
根据需要,您可以使用鼠标释放的处理程序,通过查看矩形的
x
、y
、width
和height
属性来确定所选内容。我将使用矩形
而不是按钮
。只需按照您描述的操作,但在鼠标拖动时更新矩形的大小(和位置),而不是仅在释放鼠标时添加矩形
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class SelectionRectangle extends Application {
private double mouseDownX ;
private double mouseDownY ;
@Override
public void start(Stage primaryStage) {
Rectangle selectionRectangle = new Rectangle();
selectionRectangle.setStroke(Color.BLACK);
selectionRectangle.setFill(Color.TRANSPARENT);
selectionRectangle.getStrokeDashArray().addAll(5.0, 5.0);
Pane pane = new Pane();
pane.setMinSize(600, 600);
pane.getChildren().add(selectionRectangle);
pane.setOnMousePressed(e -> {
mouseDownX = e.getX();
mouseDownY = e.getY();
selectionRectangle.setX(mouseDownX);
selectionRectangle.setY(mouseDownY);
selectionRectangle.setWidth(0);
selectionRectangle.setHeight(0);
});
pane.setOnMouseDragged(e -> {
selectionRectangle.setX(Math.min(e.getX(), mouseDownX));
selectionRectangle.setWidth(Math.abs(e.getX() - mouseDownX));
selectionRectangle.setY(Math.min(e.getY(), mouseDownY));
selectionRectangle.setHeight(Math.abs(e.getY() - mouseDownY));
});
primaryStage.setScene(new Scene(pane));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
根据需要,您可以使用鼠标释放的处理程序,通过查看矩形的
x
、y
、width
、和height
属性来确定所选内容。同样,但只需调整按钮的大小(矩形可能更好)当鼠标拖动时…不确切地知道你在问什么,但可能会有帮助。同样的事情,但只需调整鼠标拖动时按钮的大小(只是一个矩形可能更好)…不确切地知道你在问什么,但可能会有帮助。