JavaFx - 如何在按下按钮时重复操作
JavaFx - How to repeat action as long as button is pressed
我从 FXML 文件调用了控制器的 up 函数。
只要按下按钮,我就想重复这个动作。
我该如何存档。
这里以up函数为例
public void up() {
gauge.setValue(gauge.getValue()+1);
System.out.println("up");
}
这里是
的部分
<Button fx:id="btnUp" layoutX="60.0" layoutY="15.0" mnemonicParsing="false" onMousePressed="#up" onMouseReleased="#up" styleClass="style_button_up">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="btnDown" layoutX="60.0" layoutY="60.0" mnemonicParsing="false" onMousePressed="#down" onMouseReleased="#down" styleClass="style_button_down">
<font>
<Font size="18.0" />
</font>
</Button>
您需要为此使用计时器,并且 start/stop 单击按钮即可。
final AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long time) {
if (this.lastUpdate > 100) {
System.out.println("pressed");
}
this.lastUpdate = time;
}
};
final Button b = new Button();
b.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
timer.start();
} else {
timer.stop();
}
}
});
最好用这个方案
final AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long time) {
if (this.lastUpdate > 100) {
System.out.println(lastUpdate);
}
this.lastUpdate = time;
}
};
addEventFilter(MouseEvent.ANY, event -> {
if (event.getEventType() == MouseEvent.MOUSE_PRESSED)
timer.start();
if (event.getEventType() == MouseEvent.MOUSE_RELEASED) {
timer.stop();
}
});
我从 FXML 文件调用了控制器的 up 函数。 只要按下按钮,我就想重复这个动作。 我该如何存档。
这里以up函数为例
public void up() {
gauge.setValue(gauge.getValue()+1);
System.out.println("up");
}
这里是
的部分<Button fx:id="btnUp" layoutX="60.0" layoutY="15.0" mnemonicParsing="false" onMousePressed="#up" onMouseReleased="#up" styleClass="style_button_up">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="btnDown" layoutX="60.0" layoutY="60.0" mnemonicParsing="false" onMousePressed="#down" onMouseReleased="#down" styleClass="style_button_down">
<font>
<Font size="18.0" />
</font>
</Button>
您需要为此使用计时器,并且 start/stop 单击按钮即可。
final AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long time) {
if (this.lastUpdate > 100) {
System.out.println("pressed");
}
this.lastUpdate = time;
}
};
final Button b = new Button();
b.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) {
timer.start();
} else {
timer.stop();
}
}
});
最好用这个方案
final AnimationTimer timer = new AnimationTimer() {
private long lastUpdate = 0;
@Override
public void handle(long time) {
if (this.lastUpdate > 100) {
System.out.println(lastUpdate);
}
this.lastUpdate = time;
}
};
addEventFilter(MouseEvent.ANY, event -> {
if (event.getEventType() == MouseEvent.MOUSE_PRESSED)
timer.start();
if (event.getEventType() == MouseEvent.MOUSE_RELEASED) {
timer.stop();
}
});