我将如何编程方式从另一种方法点击JavaFX的一个按钮? [英] How would I programmatically click a button in JavaFX from another method?
问题描述
我有一个联网到我的手机从所谓TouchOSC应用程序接收输入一个监听器类。在这个课,我可以调用的方法,每当我preSS我的手机上的一个按钮。我需要做的就是点击一个JavaFX按钮,每当我的电脑从我的手机接收输入的方法来触发一个事件。我怎么会引发类似的东西?
I have a listener class that is networked into my phone to receive input from an application called TouchOSC. In that class, I can call methods whenever I press a button on my phone. What I need to do is to click a JavaFX button to trigger an event in that method whenever my computer receives the input from my phone. How would I trigger something like that?
推荐答案
<一个href=\"https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Button.html#fire--\"><$c$c>button.fire()$c$c>
当用户手势表示不应该出现这种ButtonBase事件时调用。
Invoked when a user gesture indicates that an event for this ButtonBase should occur.
当一个按钮被触发,按钮的<一个href=\"https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ButtonBase.html#onActionProperty\"><$c$c>onAction$c$c>事件处理函数。
When a button is fired, the button's onAction
event handler is invoked.
按钮的操作,每当按钮被触发时调用。这可能是由于在用鼠标按钮,用户点击,或由触摸事件,或由一个键preSS,或者如果显影剂编程调用火()方法
The button's action, which is invoked whenever the button is fired. This may be due to the user clicking on the button with the mouse, or by a touch event, or by a key press, or if the developer programmatically invokes the fire() method.
的样品code 的
创建一个按钮,并自动闪光四次。
Creates a button and automatically fires it four times.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.stream.IntStream;
public class RapidFire extends Application {
private static int nClicks = 0;
@Override
public void start(Stage stage) throws IOException {
// setup button and action handler.
Button button = new Button("Click Me!");
button.setOnAction(event -> {
nClicks++;
System.out.println("Clicked " + nClicks + " times.");
});
button.setPadding(new Insets(10));
button.setPrefWidth(100);
// show the button.
stage.setScene(new Scene(button));
stage.show();
// fire the button a few times in succession.
IntStream.range(0, 4).forEach(
i -> button.fire()
);
}
public static void main(String[] args) {
launch(args);
}
}
样的输出是:
Clicked 1 times.
Clicked 2 times.
Clicked 3 times.
Clicked 4 times.
这篇关于我将如何编程方式从另一种方法点击JavaFX的一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!