2017-07-14 61 views
1
同時按鍵
package javafx_pressing_simultaneous_keys; 

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.input.KeyCode; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.paint.Color; 
import javafx.scene.control.Label; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 
import javafx.beans.property.BooleanProperty; 
import javafx.beans.property.SimpleBooleanProperty; 
import javafx.beans.binding.BooleanBinding; 
import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.EventHandler; 

public class JavaFX_Pressing_Simultaneous_Keys extends Application 
{ 
    final BooleanProperty ZPressed = new SimpleBooleanProperty(false); 
    final BooleanProperty XPressed = new SimpleBooleanProperty(false); 
    final BooleanBinding ZAndXPressed = ZPressed.and(XPressed); 
    int i = 0; 

    @Override 
    public void start(Stage stage) 
    { 
    BorderPane bp = new BorderPane(); 
    bp.setStyle("-fx-background-color: linear-gradient(from 25% 25% to 100% 100%, #DC143C, #661A33)"); 
    Scene s = new Scene(bp, 340, 300); 

    Label l1 = new Label(); 
    l1.setFont(new Font("Arial", 56)); 
    l1.setTextFill(Color.CADETBLUE); 
    bp.setCenter(l1); 
    l1.setText(Integer.toString(i)); 

    // How to Respond to both Keys ("Z" and "X") pressed together. 
    ZAndXPressed.addListener(new ChangeListener<Boolean>() 
    { 
     @Override 
     public void changed(ObservableValue<? extends Boolean> obs, Boolean werePressed, Boolean arePressed) 
     { 
     i++; 
     l1.setText(Integer.toString(i)); 
     } 
    }); 

    s.setOnKeyPressed(new EventHandler<KeyEvent>() 
    { 
     @Override 
     public void handle(KeyEvent ke) 
     { 
     if (ke.getCode() == KeyCode.Z) 
      ZPressed.set(true); 
     else 
      if (ke.getCode() == KeyCode.X) 
      XPressed.set(true); 
     } 
    }); 

    s.setOnKeyReleased(new EventHandler<KeyEvent>() 
    { 
     @Override 
     public void handle(KeyEvent ke) 
     { 
     if (ke.getCode() == KeyCode.Z) 
      ZPressed.set(false); 
     else 
      if (ke.getCode() == KeyCode.X) 
      XPressed.set(false); 
     } 
    }); 

    stage.setScene(s); 
    stage.setTitle("JavaFX (Pressing Simultaneous Keys)"); 
    stage.setResizable(false); 
    stage.show(); 
    } 


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

我試圖讓一段代碼,可以允許我按2個同時鍵(例如像Z和X鍵),之後計數,我按下了組合的次數的鑰匙。但我注意到,每按一次Z和X鍵,程序就會計數兩次。任何人都可以告訴我這是否正常,或者某些EventHandler存在問題?非常感謝。按JavaFX中

回答

1

ChangeListener只知道它的對象的狀態已經改變。它不知道如何或發生了什麼對象。您現在的邏輯是每次遞增iZAndXPressed更改,無論是從真到假,還是從假到真。

試試這個:

// How to Respond to both Keys ("Z" and "X") pressed together. 
ZAndXPressed.addListener(new ChangeListener<Boolean>() 
{ 
    @Override 
    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) 
    { 
    if (newValue == true) 
    { 
     i++; 
     l1.setText(Integer.toString(i)); 
    } 
    } 
}); 
+0

非常感謝埃裏克,它幫了我很多:) –

+0

很高興我能幫忙。快樂的編碼! – Eric