2015-10-15 68 views
0

從FXML文件中,我調用控制器的up功能。 只要按下按鈕,我想要重複該操作。 如何將其歸檔。JavaFx - 只要按下按鈕,如何重複操作

這裏了功能例如

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> 

回答

2

你需要使用一個計時器的開始/巴頓點擊停止它的一部分。

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(); 
      } 

     } 
    }); 
0

這是更好地使用該解決方案

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(); 
      } 

     });