gpt4 book ai didi

单击并按住鼠标按钮,JavaFX Spinner 更改缓慢

转载 作者:行者123 更新时间:2023-12-04 17:32:08 25 4
gpt4 key购买 nike

当我单击并按住向上/向下箭头按钮时,Spinner 更新速度很慢。有没有办法提高更改速度?

当我用鼠标单击、单击、单击时,微调器值会随着我单击的速度而变化。如果我在每次按键时使用键盘上的向上/向下箭头或者如果我按住向上/向下箭头键,它也会快速变化。当我单击并按住箭头按钮时,我希望值能够快速更改。

有人知道这样做的方法吗?

最佳答案

SpinnerBehaviorSpinnerSkin每 750 毫秒触发一次更新。不幸的是,如果不使用反射来访问 private,就无法简单地设置/修改此行为。成员。因此,在没有反射的情况下执行此操作的唯一方法是使用事件过滤器以更快的速度触发更新:

private static final PseudoClass PRESSED = PseudoClass.getPseudoClass("pressed");

@Override
public void start(Stage primaryStage) {
Spinner<Integer> spinner = new Spinner(Integer.MIN_VALUE, Integer.MAX_VALUE, 0);

class IncrementHandler implements EventHandler<MouseEvent> {

private Spinner spinner;
private boolean increment;
private long startTimestamp;

private static final long DELAY = 1000l * 1000L * 750L; // 0.75 sec
private Node button;

private final AnimationTimer timer = new AnimationTimer() {

@Override
public void handle(long now) {
if (now - startTimestamp >= DELAY) {
// trigger updates every frame once the initial delay is over
if (increment) {
spinner.increment();
} else {
spinner.decrement();
}
}
}
};

@Override
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
Spinner source = (Spinner) event.getSource();
Node node = event.getPickResult().getIntersectedNode();

Boolean increment = null;
// find which kind of button was pressed and if one was pressed
while (increment == null && node != source) {
if (node.getStyleClass().contains("increment-arrow-button")) {
increment = Boolean.TRUE;
} else if (node.getStyleClass().contains("decrement-arrow-button")) {
increment = Boolean.FALSE;
} else {
node = node.getParent();
}
}
if (increment != null) {
event.consume();
source.requestFocus();
spinner = source;
this.increment = increment;

// timestamp to calculate the delay
startTimestamp = System.nanoTime();

button = node;

// update for css styling
node.pseudoClassStateChanged(PRESSED, true);

// first value update
timer.handle(startTimestamp + DELAY);

// trigger timer for more updates later
timer.start();
}
}
}

public void stop() {
timer.stop();
button.pseudoClassStateChanged(PRESSED, false);
button = null;
spinner = null;
}
}

IncrementHandler handler = new IncrementHandler();
spinner.addEventFilter(MouseEvent.MOUSE_PRESSED, handler);
spinner.addEventFilter(MouseEvent.MOUSE_RELEASED, evt -> {
if (evt.getButton() == MouseButton.PRIMARY) {
handler.stop();
}
});

Scene scene = new Scene(spinner);

primaryStage.setScene(scene);
primaryStage.show();
}

关于单击并按住鼠标按钮,JavaFX Spinner 更改缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41050085/

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