본질적으로 나는 눌려지는 동안 0.5 초마다 명령문을 실행할 GUI에 버튼을 만들려고합니다. 지금은 "차세대"라는 실제 버튼을 만들었습니다.JavaFX에서 버튼 중 누름 단추 유형을 만드는 방법은 무엇입니까?
Button nextGenButton = new Button("Next Generation");
어떻게해야합니까? 나는 어떤 종류의 이벤트 핸들러를 사용해야한다고 가정하고 있는가?
본질적으로 나는 눌려지는 동안 0.5 초마다 명령문을 실행할 GUI에 버튼을 만들려고합니다. 지금은 "차세대"라는 실제 버튼을 만들었습니다.JavaFX에서 버튼 중 누름 단추 유형을 만드는 방법은 무엇입니까?
Button nextGenButton = new Button("Next Generation");
어떻게해야합니까? 나는 어떤 종류의 이벤트 핸들러를 사용해야한다고 가정하고 있는가?
setOnMousePressed 및 setOnMouseReleased을 확인하십시오. 당신이이 처음 여부를 확인해야합니까, 그래서 버튼을 누르고있는 동안 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.
});
하는 것으로 :
코드는 다음과 같은 작은 선물을 보일 것이다.
버튼의 무장 속성을 사용할 수 있습니다. 마우스 또는 키 이벤트를 사용하지 마십시오. 그렇지 않으면 모두 체크해야합니다. 액션 이벤트를 확인해도 e 이벤트가 한 번 발생하므로 도움이되지 않습니다. 지. 마우스가 풀립니다. 무장 한 재산으로 당신은 또한 e. 지. 버튼에 포커스가있을 때 사용자가 키보드의 스페이스 키를 누를 때 버튼이 활성화된다는 것을 의미합니다. 버튼을 아래로있는 동안 증가 도착 카운터가있는 텍스트 필드를 사용하여
예 : 내 예제에서 이벤트가 모든 500ms로 해고되는 것을
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();
}
}
참고. 따라서 버튼은 최소 500ms 이상 내려야합니다. 짧은 버튼 누름에서도 이벤트를 실행하려면 ChangeListener 구현시이를 고려해야합니다. 그것은 모두 당신이 정말로 필요로하는 것에 달려 있습니다.
실제로'pressed' 속성을'armed' 속성이 아니라 실제로 사용하고 싶습니다. 그러나 그렇지 않으면 이것 역시 제가 선호하는 해결책입니다. –
내 생각도 처음 이었지만 버튼에 포커스가있을 때 스페이스 바를 클릭하여 버튼을 활성화 할 때 눌려진 속성이 작동하지 않는 것으로 나타났습니다. JavaFX 소스를 확인했습니다. 누르면 마우스 처리기에서만 사용되는 것 같습니다. – Roland
'1'버튼을 감지합니다. '2' 명령문을 실행하십시오. '3'버튼을 감지합니다. '4' 명령문 실행을 중지하십시오. – csmckelvey
@Takendarkk 이해하지만 구문과 코드가 무엇인지는 잘 모르겠습니다. – echoeida