JAVAFX + SCENE BUILDER 8.3.0 KeyEvent问题(这个神秘)
JAVAFX + SCENE BUILDER 8.3.0 problems with KeyEvent (this mysterious)
我真的不懂KeyEvent。
有时工作,有时不工作。
在我的第一个视频游戏中(我尝试制作一些简单的游戏只是为了好玩并提高我的技能)工作,我将所有键绑定到一个独特的方法因为如果我做第二种方法(Keyevent)它什么都没有。
今天我制作了一个有趣的小程序,我的妻子头像随机出现在屏幕上 运行
并且..当我执行第一个 KeyEvent 时它什么也不做,就像一个死方法。
然后我决定不惜一切代价找到原因。复制并粘贴先前程序的有效 KeyEvent 方法(位置、根、名称、节点),无需执行任何操作。有时工作有时不工作。
当我做新项目时,结果相同。有时工作有时不工作。
我不认为如果我 post 一些图片它有什么用。
有小费吗?对不起我的英语。
WINDOWS 8 JAVAFX SCENEBUILDER 日蚀
方法是这样的:(显然我在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>
控制器
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();
}
}
这更像是 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);
}
}
注意关于焦点的重要评论
控制器
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="Whosebug.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>
使图像或任何其他资源可用
我真的不懂KeyEvent。 有时工作,有时不工作。 在我的第一个视频游戏中(我尝试制作一些简单的游戏只是为了好玩并提高我的技能)工作,我将所有键绑定到一个独特的方法因为如果我做第二种方法(Keyevent)它什么都没有。
今天我制作了一个有趣的小程序,我的妻子头像随机出现在屏幕上 运行 并且..当我执行第一个 KeyEvent 时它什么也不做,就像一个死方法。
然后我决定不惜一切代价找到原因。复制并粘贴先前程序的有效 KeyEvent 方法(位置、根、名称、节点),无需执行任何操作。有时工作有时不工作。 当我做新项目时,结果相同。有时工作有时不工作。 我不认为如果我 post 一些图片它有什么用。 有小费吗?对不起我的英语。 WINDOWS 8 JAVAFX SCENEBUILDER 日蚀 方法是这样的:(显然我在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>
控制器
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();
}
}
这更像是 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);
}
}
注意关于焦点的重要评论
控制器
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="Whosebug.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>
使图像或任何其他资源可用