我一直在尝试在java中的这个程序中添加一个按钮,显示“退出”,然后关闭该程序。给出的程序制作并显示了火箭的图像,所以我不能使用另一个名为
QuitButtonExample.java
的程序给我的示例,因为每个程序都有不同的东西,并且在 QuitButtonExample
中使用类似的结构如果我删除了两部分代码,则只允许我显示火箭图像或退出按钮。以下为
Rocket.java
正常程序供参考。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
程序中对其进行了一些修改,但它给了我一个错误,指出该程序可以简化。我不希望它被简化,我只是想找到一种方法来显示火箭图像和退出按钮,并在按下关闭程序时退出按钮起作用。我已经这样做有一段时间了,我通常不确定该怎么做。我想要一些提示来帮助我了解下一步该去哪里。下面我将发布我尝试过的代码。Rocket1.java
Rocket.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();
}
}
,而从未将
Pane
添加到传递给您的
Pane
的
Parent
。下面的变体添加了按钮
Scene
;然后它然后将
HBox
添加到名为
HBox
的
Group
中,然后它成为场景图的根。 场景图,像
this一样遍历并为了清晰起见而省略:
root
经测试:
Group@56d9b476[styleClass=root]
Polygon[…]
Polygon[[…]
Polyline[…]
HBox@25afbfbc
Button@36457869[…]'Quit'
Text[text="Quit", x=0.0, y=0.0, …]
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.
//--------------------------------------------------------------------
@Override
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(btn);
exitButton.setPadding(new Insets(25));
root.getChildren().add(exitButton);
Scene scene = new Scene(root, 400, 400, Color.BLACK);
primaryStage.setTitle("Rocket");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch();
}
}