【发布时间】:2018-04-29 13:43:32
【问题描述】:
大家好,我有一个问题,我正在使用 eclipse、javafx 和场景生成器 当我使用箭头(右左......)时,我想移动椭圆(圆圈) 这是我的代码(我尝试了 10 件事,但似乎没有任何效果)这是我尝试过的最后一个想法,如果有人可以帮助谢谢:)
这是我的主要课程:
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语句,改变我的主要,改变fxml但没有奏效)
【问题讨论】:
-
我已经看过这个,它不谈论 FXML 和场景构建器,它只是关于 JavaFx,感谢您抽出宝贵时间
标签: java javafx scenebuilder