问题
hello guys i have a problem, i am using eclipse , javafx and scene builder i want to move the ellipse( the circle) when i use arrow ( right left....) here is my code ( i tried like 10 things but nothing seems to work ) this is the last think i tried if any one can help thank :)
This is my Main class :
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
loader.setController(new SampleController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}}
This is my controller class :
package application;
import javafx.fxml.FXML;
import javafx.scene.input.KeyEvent;
import javafx.scene.shape.Ellipse;
public class SampleController {
@FXML
private Ellipse Test;
@FXML
public void keyPressed(KeyEvent key){
if(key.getCode().isArrowKey()){
System.out.println("rlrlrl");
}}}
My FXML File :
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.Double?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.paint.LinearGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.shape.Ellipse?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.shape.Rectangle?>
<Pane onKeyPressed="#keyPressed" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0"
prefWidth="800.0" xmlns="http://javafx.com/javafx/9.0.1"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<Polygon fill="DODGERBLUE" layoutX="507.0" layoutY="587.0" rotate="180.0"
stroke="BLACK" strokeType="INSIDE">
<points>
<Double fx:value="-50.0" />
<Double fx:value="40.0" />
<Double fx:value="50.0" />
<Double fx:value="40.0" />
<Double fx:value="0.0" />
<Double fx:value="-60.0" />
</points>
</Polygon>
<Rectangle arcHeight="5.0" arcWidth="5.0" height="200.0" layoutX="-3.0" layoutY="577.0" stroke="BLACK" strokeType="INSIDE" width="809.0">
<fill>
<LinearGradient endX="1.0" endY="1.0">
<stops>
<Stop color="#4c6b33" />
<Stop color="WHITE" offset="1.0" />
</stops>
</LinearGradient>
</fill>
</Rectangle>
<Ellipse fx:id="Test" fill="DODGERBLUE" layoutX="60.0" layoutY="558.0"
radiusX="23.0" radiusY="19.0" stroke="BLACK" strokeType="INSIDE" />
</children>
</Pane>
i tried everything on every forums ; (the switch statement,changing my main,changing on the fxml but none worked)
回答1:
you have to use scene instead Pane for detecting keypressed
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/application/Sample.fxml"));
Scene scene = new Scene(root);
scene.setOnKeyPressed(event -> {
String codeString = event.getCode().toString();
if (event.getCode().isArrowKey()) {
System.out.println("rlrlrl");
}
});
stage.setScene(scene);
stage.show();
}
回答2:
In code
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
loader.setController(new SampleController());
primaryStage.setScene(new Scene(loader.load()));
primaryStage.show()
Change this to
FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
loader.setController(new SampleController());
Pane root = loader.load();
Scene scene = new Scene(root)
primaryStage.setScene(scene);
primaryStage.show()
scene.getRoot().requestFocus(); // important
来源:https://stackoverflow.com/questions/50087230/scene-builder-event-handler-key-listener