如何在JavaFX中创建按下按钮时运行的类型按钮?

如何在JavaFX中创建按下按钮时运行的类型按钮?,java,user-interface,javafx,Java,User Interface,Javafx,本质上,我试图在GUI上创建一个按钮,当按下按钮时,它将每隔0.5秒运行一些语句。目前,我已经创建了名为“下一代”的实际按钮 在这之后我将如何继续?我假设我必须使用某种类型的事件处理程序?签出并删除 您的代码看起来有点像这样: final Button btn = new Button("Click me!"); btn.setOnMousePressed((event) -> { /** * Check if this is the first time this ha

本质上,我试图在GUI上创建一个按钮,当按下按钮时,它将每隔0.5秒运行一些语句。目前,我已经创建了名为“下一代”的实际按钮

在这之后我将如何继续?我假设我必须使用某种类型的事件处理程序?

签出并删除

您的代码看起来有点像这样:

final Button btn = new Button("Click me!");
btn.setOnMousePressed((event) -> {
    /**
     * Check if this is the first time this handler runs.
     * - If so, start a timer that runs every 0.5 seconds.
     * - If not, do nothing. The timer is already running.
     */
});
btn.setOnMouseReleased((event) -> {
    //Stop the timer.
});
请注意,按下按钮时会反复调用
onMousePressed
,因此您必须检查这是否是第一次。

签出并删除

您的代码看起来有点像这样:

final Button btn = new Button("Click me!");
btn.setOnMousePressed((event) -> {
    /**
     * Check if this is the first time this handler runs.
     * - If so, start a timer that runs every 0.5 seconds.
     * - If not, do nothing. The timer is already running.
     */
});
btn.setOnMouseReleased((event) -> {
    //Stop the timer.
});

请注意,按下按钮时会重复调用
onMousePressed
,因此您必须检查这是否是第一次。

您可以使用按钮的armed属性。不要使用鼠标或按键事件,否则必须检查所有事件。检查操作事件也不会对您有所帮助,因为它在e。G鼠标被释放。你的武装财产也包括在内。G当用户按下键盘上的空格键且按钮具有焦点时,按钮被激活

示例使用一个文本字段,该字段的计数器在按钮按下时增加:

public class ButtonDemo extends Application {

    // counter which increases during button armed state
    int counter = 0;

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

    @Override
    public void start(Stage primaryStage) {

        // create textfield with the counter value as text
        TextField textField = new TextField();
        textField.setAlignment( Pos.CENTER_RIGHT);
        textField.setText( String.valueOf(counter));

        // timeline that gets started and stopped depending on the armed state of the button. event is fired every 500ms
        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(500), actionEvent -> { counter++; textField.setText( String.valueOf(counter)); }));
        timeline.setCycleCount(Animation.INDEFINITE);

        // button which starts/stops the timeline depending on the armed state
        Button button = new Button( "ClickMe");
        button.armedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                System.out.println( "armed: " + newValue);

                if( newValue) {

                    timeline.play();

                } else {

                    timeline.stop();

                }

            }
        });

        // container for nodes
        HBox hBox = new HBox();
        hBox.setSpacing(5.0);
        hBox.getChildren().addAll( button, textField);


        primaryStage.setScene(new Scene( hBox, 640, 480));
        primaryStage.show();
    }

}
公共类按钮Demo扩展应用程序{
//在按钮启用状态下增加的计数器
int计数器=0;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//创建计数器值为文本的textfield
TextField TextField=新的TextField();
textField.setAlignment(右中位置);
textField.setText(String.valueOf(counter));
//根据按钮的待命状态启动和停止的时间线。事件每500毫秒触发一次
Timeline Timeline=新的时间线(新的关键帧(Duration.millis(500),actionEvent->{counter++;textField.setText(String.valueOf(counter));});
timeline.setCycleCount(Animation.unfinite);
//根据待命状态启动/停止时间线的按钮
按钮按钮=新按钮(“单击我”);
button.armedProperty().addListener(新的ChangeListener()){
@凌驾

更改公众假期(ObservalEvalue您可以使用按钮的防护属性。不要使用鼠标或按键事件,否则您必须检查所有事件。检查动作事件也不会对您有帮助,因为一旦释放鼠标,它就会被触发。使用防护属性,您还可以覆盖,例如,当用户按下空格键时,按钮会被激活在键盘上,按钮具有焦点

示例使用一个文本字段,该字段的计数器在按钮按下时增加:

public class ButtonDemo extends Application {

    // counter which increases during button armed state
    int counter = 0;

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

    @Override
    public void start(Stage primaryStage) {

        // create textfield with the counter value as text
        TextField textField = new TextField();
        textField.setAlignment( Pos.CENTER_RIGHT);
        textField.setText( String.valueOf(counter));

        // timeline that gets started and stopped depending on the armed state of the button. event is fired every 500ms
        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(500), actionEvent -> { counter++; textField.setText( String.valueOf(counter)); }));
        timeline.setCycleCount(Animation.INDEFINITE);

        // button which starts/stops the timeline depending on the armed state
        Button button = new Button( "ClickMe");
        button.armedProperty().addListener(new ChangeListener<Boolean>() {

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                System.out.println( "armed: " + newValue);

                if( newValue) {

                    timeline.play();

                } else {

                    timeline.stop();

                }

            }
        });

        // container for nodes
        HBox hBox = new HBox();
        hBox.setSpacing(5.0);
        hBox.getChildren().addAll( button, textField);


        primaryStage.setScene(new Scene( hBox, 640, 480));
        primaryStage.show();
    }

}
公共类按钮Demo扩展应用程序{
//在按钮启用状态下增加的计数器
int计数器=0;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//创建计数器值为文本的textfield
TextField TextField=新的TextField();
textField.setAlignment(右中位置);
textField.setText(String.valueOf(counter));
//根据按钮的待命状态启动和停止的时间线。事件每500毫秒触发一次
Timeline Timeline=新的时间线(新的关键帧(Duration.millis(500),actionEvent->{counter++;textField.setText(String.valueOf(counter));});
timeline.setCycleCount(Animation.unfinite);
//根据待命状态启动/停止时间线的按钮
按钮按钮=新按钮(“单击我”);
button.armedProperty().addListener(新的ChangeListener()){
@凌驾

更改公众假期(ObservableValue
1
Detect button down.
2
Start running语句。
3
Detect button up.
4
Stop running语句。
3
Detectt按钮向上。
4
停止运行语句。@takendark我理解,但我不确定语法和代码是什么。啊,这是有意义的。谢谢。不客气。祝你好运!这只包括鼠标事件。按钮动作可以以不同的方式触发。检查我的解决方案。啊,这是有意义的。谢谢。不客气。祝你好运!这只涉及鼠标事件。按钮操作可以以不同的方式触发。请检查我的解决方案。我实际上会使用
pressed
属性,而不是
armed
属性,但除此之外,这也是我更喜欢的解决方案。这也是我的第一个想法,但我注意到当你操作时,pressed属性不起作用当按钮有焦点时,通过单击空格键来激活按钮。我查看了JavaFX源代码。pressed似乎仅用于鼠标处理程序。我实际上使用了
pressed
属性,而不是
armed
属性,但除此之外,这也是我更喜欢的解决方案。这也是我的第一个想法,但我注意到当按钮有焦点时,通过单击空格键激活按钮时,essed属性不起作用。我检查了JavaFX源代码。pressed似乎仅用于鼠标处理程序。