JAVAFX + SCENE BUILDER与KeyEvent的8.3.0问题(这个神秘的)

时间:2017-08-21 20:04:54

标签: java javafx scenebuilder keyevent

我真的不明白KeyEvent。 有时工作,有时不工作。 在我的第一个视频游戏中(我试图制作一些简单的游戏只是为了娱乐并提高我的技能)工作,我将所有键盘绑定到一个独特的方法,因为如果我做第二种方法(Keyevent)它并不是什么。

今天我制作了一个有趣的节目,我的妻子头随机在屏幕上运行    并且......当我做第一个KeyEvent时,它什么都不做,就像死方法一样。

然后我决定不惜一切代价找到原因。复制并粘贴以前程序的工作KeyEvent方法(位置,根,名称,节点),无所事事。有时候工作不会。 当我做新项目相同的结果。有时候工作不会。 我不认为如果我发布一些图片它服务的东西。 有小费吗?抱歉我的英语 WINDOWS 8 JAVAFX SCENEBUILDER ECLIPSE 方法是这样的:(显然我在scenebuilder中写了方法的名称)

public void Press(KeyEvent event) {

        if(event.getCode()==KeyCode.ENTER) {action14();}
        else {\\ do something  }event.consume();
}

event.consume();只是一个尝试

这里是代码:

主要

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.input.KeyCode;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root =  FXMLLoader.load(getClass().getResource("prova1.fxml"));
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            primaryStage.setScene(scene);

            primaryStage.show();
            scene.getRoot().requestFocus();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

}

FXML

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane focusTraversable="true" prefHeight="344.0" prefWidth="384.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controller.Mycontroller">
   <children>
      <ImageView fitHeight="1080.0" fitWidth="1980.0" focusTraversable="true" layoutX="-39.0" layoutY="-23.0">
         <image>
            <Image url="@grass123.jpg" />
         </image>
      </ImageView>
      <ImageView fx:id="war" focusTraversable="true" layoutX="244.0" layoutY="96.0" onKeyPressed="#Press123">
         <image>
            <Image url="@giphy-tumblr.gif" />
         </image>
      </ImageView>
      <ImageView fx:id="alla" fitHeight="102.0" fitWidth="105.0" focusTraversable="true" layoutX="545.0" layoutY="574.0" onMouseClicked="#allaclic" pickOnBounds="true">
         <image>
            <Image url="@faccino.png" />
         </image>
      </ImageView>
      <ImageView fx:id="base" fitHeight="20.0" fitWidth="8000.0" layoutX="-671.0" layoutY="666.0">
         <image>
            <Image url="@../../../../Desktop/barra.png" />
         </image>
      </ImageView>
   </children>
</AnchorPane>

CONTROLLER

import java.net.URL;
import java.util.ResourceBundle;
import javafx.animation.Animation;
import javafx.animation.AnimationTimer;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
public class Mycontroller<I> implements Initializable{
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
    }
    public void Press123(KeyEvent y) {
        if(y.getCode()==KeyCode.ENTER) {war.relocate (50,50); }
        else {}
    }
    @FXML
    public  ImageView alla;
    @FXML 
    public  ImageView war;
    @FXML Circle cir=new Circle(40,40,40);
    public int random() {
        int a=0;a=(int) (Math.random()*300-300); 
        return a;}
    public void allaclic(MouseEvent o) {
        AnimationTimer time=new AnimationTimer() {
            @Override
            public void handle(long now) {
                int x=0;x+=1;int y=0;y+=1;
                saluto(x,y);
            }
        };time.start();
        TranslateTransition tra=new TranslateTransition();
        tra.setNode(alla);
        tra.setToX(random());
        tra.setToY(random());
        tra.setCycleCount(1);
        tra.setAutoReverse(true);
        tra.setDuration(Duration.seconds(1.5));
        tra.setOnFinished(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent t) {allaclic(o);}
        });
        tra.play();o.consume();
        FadeTransition trans = new FadeTransition(Duration.seconds(1), (alla
                ));
        trans.setFromValue(1.0);
        trans.setToValue(.00);
        trans.setCycleCount(FadeTransition.INDEFINITE);
        trans.setAutoReverse(true);
        trans.setOnFinished(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent t) {t.consume();}
        });
        trans.play();
    }
    public void rotation() {
        RotateTransition rotate=new RotateTransition();
        rotate.setNode(alla);
        rotate.setByAngle(random());rotate.setFromAngle(random());
        rotate.setCycleCount(Animation.INDEFINITE);
        rotate.setAutoReverse(true);
        rotate.play();
        int g=0;int y=0;saluto(g,y);
    }
    public void saluto(int x,int y) {
        if(alla.getBoundsInParent().intersects(163,160,15 ,75)) {  //something;
        }
    }
    //something}
    public void tempohey() {
        Timeline time=new Timeline();
        time.setAutoReverse(false);
        time.setCycleCount(1);
        KeyValue kv=new KeyValue(war.layoutXProperty(),200);
        KeyFrame kf=new KeyFrame(Duration.millis(1500),kv);
        time.getKeyFrames().add(kf);time.play();
    }
}   

1 个答案:

答案 0 :(得分:0)

这更像是mcve,并演示了一个有效的键事件处理程序:

public class DrawImage extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

        Parent root =  FXMLLoader.load(getClass().getResource("DrawImage.fxml"));
        Scene scene = new Scene(root);

        primaryStage.setScene(scene);
        primaryStage.show();
        scene.getRoot().requestFocus();//key event needs focus
    }

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

请注意有关重点的重要评论

<强> CONTROLLER

public class DrawImageController{

    @FXML ImageView war;

    @FXML
    public void press123(KeyEvent event){

        System.out.println("Key Pressed"); // print a message
        if(event.getCode()==KeyCode.ENTER) {war.relocate (50,50); }
    }
}

<强> FXML

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="100.0" prefWidth="100.0" 
onKeyPressed="#press123"
xmlns="http://javafx.com/javafx/8.0.111" 
xmlns:fx="http://javafx.com/fxml/1" 

fx:controller="stackoverflow.DrawImageController">

    <children>
        <ImageView fx:id="war" fitHeight="50.0" fitWidth="50.0" 
                layoutX="50.0" layoutY="20.0" pickOnBounds="true" preserveRatio="true">
            <image>
                <Image url="http://icons.iconarchive.com/icons/iconka/meow-2/64/cat-rascal-icon.png" />
            </image>
        </ImageView>
    </children>
</AnchorPane>

使其可以制作图像或任何其他可用资源