我一直在尝试在java中的这个程序中添加一个按钮,显示“退出”,然后关闭该程序。给出的程序制作并显示了火箭的图像,所以我不能使用另一个名为 QuitButtonExample.java 的程序给我的示例,因为每个程序都有不同的东西,并且仅在 Rocket.java 中的 QuitButtonExample 中使用类似的结构如果我删除了两部分代码,则可以显示火箭图像或退出按钮。下面是正常的Rocket.java程序,供参考。
Rocket.java:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline;
import javafx.stage.Stage;
public class Rocket extends Application
{
//--------------------------------------------------------------------
// Displays a rocket lifting off. The rocket and hatch are polygons
// and the flame is a polyline.
//--------------------------------------------------------------------
public void start(Stage primaryStage)
{
double[] hullPoints = {200, 25, 240, 60, 240, 230, 270, 260,
270, 300, 140, 300, 140, 260, 160, 230, 160, 60};
Polygon rocket = new Polygon(hullPoints);
rocket.setFill(Color.BEIGE);
double[] hatchPoints = {185, 70, 215, 70, 220, 120, 180, 120};
Polygon hatch = new Polygon(hatchPoints);
hatch.setFill(Color.MAROON);
double[] flamePoints = {142, 310, 142, 330, 150, 325, 155, 380,
165, 340, 175, 360, 190, 350, 200, 375, 215, 330, 220, 360,
225, 355, 230, 370, 240, 340, 255, 370, 260, 335, 268, 340,
268, 310};
Polyline flame = new Polyline(flamePoints);
flame.setStroke(Color.RED);
flame.setStrokeWidth(3);
Group root = new Group(rocket, hatch, flame);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
primaryStage.setTitle("Rocket");
primaryStage.setScene(scene);
primaryStage.show();
}
}
我使用了 QuitButtonExample.java 程序中的一些代码,并在 Rocket.java 程序中对其进行了一些修改,但它给了我一个错误,指出该程序可以简化。我不希望它被简化,我只是想找到一种方法来显示火箭图像和退出按钮,并在按下关闭程序时退出按钮起作用。我已经这样做有一段时间了,我通常不确定该怎么做。我想要一些提示来帮助我了解下一步该去哪里。下面我将发布我尝试过的代码。
Rocket1.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline;
import javafx.stage.Stage;
public class Rocket extends Application
{
//--------------------------------------------------------------------
// Displays a rocket lifting off. The rocket and hatch are polygons
// and the flame is a polyline.
//--------------------------------------------------------------------
public void start(Stage primaryStage)
{
double[] hullPoints = {200, 25, 240, 60, 240, 230, 270, 260,
270, 300, 140, 300, 140, 260, 160, 230, 160, 60};
Polygon rocket = new Polygon(hullPoints);
rocket.setFill(Color.BEIGE);
double[] hatchPoints = {185, 70, 215, 70, 220, 120, 180, 120};
Polygon hatch = new Polygon(hatchPoints);
hatch.setFill(Color.MAROON);
double[] flamePoints = {142, 310, 142, 330, 150, 325, 155, 380,
165, 340, 175, 360, 190, 350, 200, 375, 215, 330, 220, 360,
225, 355, 230, 370, 240, 340, 255, 370, 260, 335, 268, 340,
268, 310};
Polyline flame = new Polyline(flamePoints);
flame.setStroke(Color.RED);
flame.setStrokeWidth(3);
Group root = new Group(rocket, hatch, flame);
Button btn = new Button();
btn.setText("Quit");
btn.setOnAction((ActionEvent event) -> {
Platform.exit();
});
HBox exitButton = new HBox();
exitButton.setPadding(new Insets(25));
exitButton.getChildren().add(btn);
Scene scene = new Scene(root, exitButton, 400, 400, Color.BLACK);
primaryStage.setTitle("Rocket");
primaryStage.setScene(scene);
primaryStage.show();
}
}
我的一个朋友帮助我解决了我的问题以及我的程序发生了什么。最初我并没有太偏离,但我只需要改变 3 件事。