我创建了一个在画布中创建矩形的程序,并使用键盘箭头键的事件处理程序移动。还有一个选项框,选项1-5表示所选的速度。速度为1表示矩形每按键移动1个像素,速度2表示矩形每按键移动2个像素,依此类推。我的代码在netbeans中编译并正确运行,但是当我尝试在PuTTY中编译它时,我无法弄清楚我做错了什么。
我第一次收到的错误是
Note: AnimatedRectangle.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
所以我用-Xlint重新编译,得到以下输出:
AnimatedRectangle.java:71: warning: [unchecked] unchecked call to ChoiceBox(ObservableList<T>) as a member of the raw type ChoiceBox cb = new ChoiceBox(FXCollections.observableArrayList ( where T is a type variable: T extends Object declared in class ChoiceBox AnimatedRectangle.java:75: warning [unchecked] unchecked call to setValue(T) as a member of the raw type ChoiceBox cb.setValue("1"); where T is a type variable: T extends Object declared in class ChoiceBox 2 warnings
这是我的全部代码:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ChoiceBox;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class AnimatedRectangle extends Application {
Group root;
Canvas canvas;
Scene scene;
GraphicsContext gc;
ChoiceBox cb;
String cbInput;
int speed;
int x, y, width, height;
int n = x + 1;
String userInput;
@Override
public void start(Stage primaryStage) {
//declare rectangle variables
width = 100;
height = 50;
y = 150;
x = 150;
n = x + 1;
//declare a new group and a new canvas
root = new Group();
canvas = new Canvas(400, 400);
//set the scene and make the background color white
scene = new Scene(root, 400, 400, Color.WHITE);
//create a 2D graphics context
gc = canvas.getGraphicsContext2D();
//create a green rectangle
gc.setFill(Color.GREEN);
gc.fillRect(150, 150, 100, 50);
//create a choice box with options 1, 2, 3, 4, and 5
cb = new ChoiceBox(FXCollections.observableArrayList(
"1","2","3","4","5"));
//set the pre-determined value to 1
cb.setValue("1");
//add the canvas and the choice box to the group
root.getChildren().add(canvas);
root.getChildren().add(cb);
//add an event handler to the group.
//This event handler will first decide which choice box
//is selected, and according to that, a series of events will occur.
//The rectangle will either move right or left depending on
//which key is pressed. The choice box options determine the
//speed in which the rectangle will move. 1 meaning 1 pixel per move,
//and 5 meaning 5 pixels per move, and so on.
root.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent event){
userInput = cb.getValue().toString();
if (userInput == "1"){
switch (event.getCode()){
case RIGHT: //gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n+1, y, width, height);
x = n + 1;
n++;
break;
case LEFT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n-1, y, width, height);
x = n - 1;
n--;
break;
}
}
if (userInput == "2"){
switch (event.getCode()){
case RIGHT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n+2, y, width, height);
x = n + 2;
n = n + 2;
break;
case LEFT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n-2, y, width, height);
x = n - 2;
n = n - 2;
break;
}
}
if (userInput == "3"){
switch (event.getCode()){
case RIGHT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n+3, y, width, height);
x = n + 3;
n = n + 3;
break;
case LEFT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n-3, y, width, height);
x = n - 3;
n = n - 3;
break;
}
}
if (userInput == "4"){
switch (event.getCode()){
case RIGHT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n+4, y, width, height);
x = n + 4;
n = n + 4;
break;
case LEFT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n-4, y, width, height);
x = n - 4;
n = n - 4;
break;
}
}
if (userInput == "5"){
switch (event.getCode()){
case RIGHT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n+5, y, width, height);
x = n + 5;
n = n + 5;
break;
case LEFT: gc.fillRect(x, y, width, height);
gc.clearRect(x, y, width, height);
gc.fillRect(n-5, y, width, height);
x = n - 5;
n = n -5;
break;
}
}
}
});
//set the title & scene of the stage
primaryStage.setTitle("Move rectangle");
primaryStage.setScene(scene);
primaryStage.show();
}
//the main class
public static void main(String[] args) {
launch(args);
}
}
我真的可以使用我能得到的所有帮助!如果您在我的代码中看到错误,请发表评论并告诉我您的想法!谢谢!