目前,技术学院要求我设计一个机器人并为其提供一些功能。这些功能之一是,单击“ Revive”按钮后,机器人将再次出现(通过右键单击机器人本身三下而消失了!)。
我的代码无法正常工作。单击该按钮似乎冻结,不再起作用!
我该如何解决?
这是我的代码:
package at.ac.fhcampuswien;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class HelloRobot extends Application {
private int counter = 0;
private static final int BOARD_SIZE = 600;
@Override
public void start(Stage primaryStage) throws Exception{
Group root = new Group();
Group robot = FXMLLoader.load(getClass().getResource("robot.fxml"));
root.getChildren().add(robot);
Label label = new Label("");
label.setPrefWidth(BOARD_SIZE);
label.setAlignment(Pos.CENTER);
root.getChildren().addAll(label);
Button revive = new Button("Revive!");
revive.setOnAction(actionEvent -> {
if(counter < 3) {
label.setText("!Dead yet!?");
} else {
root.getChildren().add(robot);
label.setText("Be nice this time");
counter = 0;
}
/*
for(Node node: root.getChildren()){
if(robot != null)
{
label.setText("!Dead yet!?");
}
}
if(counter == 3) {
root.getChildren().add(robot);
label.setText("Be nice this time");
counter = 0;
}*/
});
root.getChildren().add(revive);
robot.setOnMousePressed(mouseEvent -> {
if (mouseEvent.isSecondaryButtonDown()) {
if(counter == 0) {
label.setText("Oooooouch!");
counter++;
}
else if(counter == 1){
label.setText("Please no!");
counter++;
}
else if(counter == 2){
root.getChildren().remove(robot);
counter++;
}
}
});
Scene scene = new Scene(root, BOARD_SIZE, BOARD_SIZE);
primaryStage.setTitle("Hello Robot");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是robot.fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Group?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Circle?>
<?import javafx.scene.shape.CubicCurve?>
<?import javafx.scene.shape.Ellipse?>
<?import javafx.scene.shape.Polygon?>
<?import javafx.scene.shape.Rectangle?>
<Group fx:id="robot" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="at.ac.fhcampuswien.RobotController">
<children>
<AnchorPane prefHeight="600.0" prefWidth="600.0">
<children>
<Polygon fill="#ff1f1f" layoutX="349.0" layoutY="425.0" points="-18.0, 47.0, 51.0, 47.0, 0.0, -60.0" stroke="BLACK" strokeType="INSIDE" />
<Polygon fill="#ff1f1f" layoutX="247.0" layoutY="433.0" points="-47.0, 40.0, 25.0, 40.0, 8.0, -70.0" stroke="BLACK" strokeType="INSIDE" />
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="200.0" layoutX="200.0" layoutY="163.0" stroke="BLACK" strokeType="INSIDE" width="200.0" />
<Circle fill="#eaff1f" layoutX="300.0" layoutY="163.0" radius="100.0" stroke="BLACK" strokeType="INSIDE" />
<Ellipse fill="#1eff3e" layoutX="264.0" layoutY="117.0" radiusX="36.0" radiusY="14.0" stroke="BLACK" strokeType="INSIDE" />
<Ellipse fill="#1eff3e" layoutX="336.0" layoutY="117.0" radiusX="36.0" radiusY="14.0" stroke="BLACK" strokeType="INSIDE" />
<Circle fill="DODGERBLUE" layoutX="300.0" layoutY="163.0" radius="22.0" stroke="BLACK" strokeType="INSIDE" />
<CubicCurve controlX1="-57.0" controlX2="56.0" controlY1="41.0" controlY2="41.0" endX="56.0" fill="DODGERBLUE" layoutX="301.0" layoutY="203.0" startX="-57.0" stroke="BLACK" />
<Polygon fill="DODGERBLUE" layoutX="170.0" layoutY="262.0" points="30.0, 30.0, 30.0, -34.0, -43.0, 0.0" stroke="BLACK" strokeType="INSIDE" />
<Polygon fill="DODGERBLUE" layoutX="370.0" layoutY="265.0" points="30.0, 30.0, 30.0, -34.0, 103.0, -4.0" stroke="BLACK" strokeType="INSIDE" />
</children>
</AnchorPane>
</children>
</Group>