gpt4 book ai didi

拖动手势 Activity 时未引发 JavaFX KeyEvent

转载 作者:行者123 更新时间:2023-12-03 23:13:36 26 4
gpt4 key购买 nike

所以我的问题是,我正在实现一个 UI 创建工具,该工具需要使用边缘上的拖动手势来调整元素的大小。在检查修改键是否被按下时进行此拖动时会出现问题,即实现统一缩放。拖动手势处于 Activity 状态时永远不会引发键事件,因此我无法在拖动过程中激活/停用此状态,这显然不是理想的!我的问题是,在 JavaFX 中是否存在某些原因导致出现这种情况?有什么解决方法吗?我可以访问原始键盘状态,即没有事件回调吗?

最佳答案

这对我来说似乎工作得很好:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tooltip;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class DraggableStretchableNodeExample extends Application {
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();

Rectangle rect = new Rectangle(50, 50, 50, 50);
rect.setFill(Color.CORNFLOWERBLUE);

Tooltip tooltip = new Tooltip("Drag to move, Z+Drag to stretch");
Tooltip.install(rect, tooltip);

root.getChildren().add(rect);

enableDragging(rect);

Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
}

private void enableDragging(Rectangle rect) {

// A mutable struct-like nested class for the last known mouse location:
class MouseLocation {
double x, y ;
void set(double x, double y) {
this.x=x ;
this.y=y ;
}
}
MouseLocation mouseLocation = new MouseLocation();

// Another for the 'Z' key state:
class Zstate {
boolean pressed ;
}
Zstate zState = new Zstate();

rect.setOnMousePressed(event -> mouseLocation.set(event.getX(), event.getY()));
rect.setOnMouseDragged(event -> {
if (zState.pressed) {
stretch(rect, event.getX(), event.getY(), event.getX()-mouseLocation.x, event.getY()-mouseLocation.y);
} else {
move(rect, event.getX()-mouseLocation.x, event.getY()-mouseLocation.y);
}
mouseLocation.set(event.getX(), event.getY());
});

rect.setFocusTraversable(true);
rect.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.Z) {
zState.pressed = true ;
}
});
rect.setOnKeyReleased(event -> {
if (event.getCode() == KeyCode.Z) {
zState.pressed = false ;
}
});

}

private void stretch(Rectangle rect, double x, double y, double deltaX, double deltaY) {

if (x < rect.getX() + rect.getWidth() / 2) {
// if the mouse is in the left half, stretch left by adjusting x and width:
rect.setX(rect.getX() + deltaX);
rect.setWidth(rect.getWidth() - deltaX);
} else {
// if in the right half, just stretch by adjusting the width:
rect.setWidth(rect.getWidth() + deltaX);
}

// same vertically:
if (y <rect.getY() + rect.getHeight() / 2) {
rect.setY(rect.getY() + deltaY);
rect.setHeight(rect.getHeight() - deltaY);
} else {
rect.setHeight(rect.getHeight() + deltaY);
}

}

private void move(Rectangle rect, double deltaX, double deltaY) {
rect.setX(rect.getX()+deltaX);
rect.setY(rect.getY()+deltaY);
}

public static void main(String[] args) {
launch(args);
}
}

关于拖动手势 Activity 时未引发 JavaFX KeyEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23399283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com