场景构建器事件处理程序(Key listener) [英] Scene builder event handler ( Key listener)

查看:209
本文介绍了场景构建器事件处理程序(Key listener)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我有问题,我正在使用eclipse,javafx和场景构建器
i想要移动椭圆(圆圈)当我使用箭头(右边......)
这里是我的代码(我尝试了10件事,但似乎没有任何工作)这是我试过的最后一个想法,如果任何人可以帮助谢谢:)

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 :)

这是我的主类:

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

这是我的控制器类:

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

我的FXML文件:

<?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>

我在每个论坛上尝试了一切; (switch语句,改变我的main,改变fxml但没有工作)

i tried everything on every forums ; (the switch statement,changing my main,changing on the fxml but none worked)

推荐答案

代码

  FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Sample.fxml"));
  loader.setController(new SampleController());
  primaryStage.setScene(new Scene(loader.load()));
  primaryStage.show()

将此更改为

   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

这篇关于场景构建器事件处理程序(Key listener)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆