invoke 按钮点击,我将如何编程方式从另一种方法点击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?

解决方案Invoked when a user gesture indicates that an event for this ButtonBase should occur.

When a button is fired, the button's onAction event handler is invoked.

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.

Sample 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);

}

}

Output of the sample is:

Clicked 1 times.

Clicked 2 times.

Clicked 3 times.

Clicked 4 times.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值