除非我在JAVA/JavaFX中先单独添加文本,否则窗格不会添加所有子项
我正在做一个家庭作业,我正在用Java和JavaFX制作一个圆柱体。圆柱体将根据窗口的大小重新调整大小 我首先在顶部创建一个椭圆,两条垂直线,在底部创建两个圆弧(一条虚线)。我已将它们绑定到窗格上,因此它们会随着窗口大小的改变而改变 当我试着运行它时,程序编译得很好(在Intelli-J中),新的Java窗口出现了,但程序似乎挂在那里。我无法访问该窗口,只能在Mac电脑的程序栏中看到它 出于某种原因,当我在添加所有形状之前将文本单独添加到窗格中时,效果如何 代码如下。任何帮助都将不胜感激除非我在JAVA/JavaFX中先单独添加文本,否则窗格不会添加所有子项,java,javafx,Java,Javafx,我正在做一个家庭作业,我正在用Java和JavaFX制作一个圆柱体。圆柱体将根据窗口的大小重新调整大小 我首先在顶部创建一个椭圆,两条垂直线,在底部创建两个圆弧(一条虚线)。我已将它们绑定到窗格上,因此它们会随着窗口大小的改变而改变 当我试着运行它时,程序编译得很好(在Intelli-J中),新的Java窗口出现了,但程序似乎挂在那里。我无法访问该窗口,只能在Mac电脑的程序栏中看到它 出于某种原因,当我在添加所有形状之前将文本单独添加到窗格中时,效果如何 代码如下。任何帮助都将不胜感激 imp
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class CylinderResize extends Application {
@Override
public void start(Stage primaryStage) {
//Create Pane
Pane pane = new Pane();
Scene scene = new Scene(pane);
//Create Elipse for top
Ellipse ellipse = new Ellipse();
//Make the Center X property half of the pane width
ellipse.centerXProperty().bind(pane.widthProperty().divide(2));
//Height starts 1/3 the way down
ellipse.centerYProperty().bind(pane.heightProperty().divide(3));
//X Radius is 1/4 the width property and y radius is 1/8 WIDTH property
ellipse.radiusXProperty().bind(pane.widthProperty().divide(4));
ellipse.radiusYProperty().bind(pane.heightProperty().divide(8));
ellipse.setStroke(Color.BLACK);
ellipse.setFill(Color.WHITE);
//Create Solid arch for bottom
Arc solidArc = new Arc();
solidArc.centerXProperty().bind(pane.widthProperty().divide(2));
solidArc.centerYProperty().bind(pane.heightProperty().multiply(2).divide(3));
solidArc.radiusXProperty().bind(pane.widthProperty().divide(4));
solidArc.radiusYProperty().bind(pane.heightProperty().divide(8));
solidArc.setStartAngle(180);
solidArc.setLength(180);
solidArc.setType(ArcType.OPEN);
solidArc.setStroke(Color.BLACK);
solidArc.setFill(Color.WHITE);
//Create dashed line for bottom
Arc dashedArc = new Arc();
dashedArc.centerXProperty().bind(pane.widthProperty().divide(2));
dashedArc.centerYProperty().bind(pane.heightProperty().multiply(2).divide(3));
dashedArc.radiusXProperty().bind(pane.widthProperty().divide(4));
dashedArc.radiusYProperty().bind(pane.heightProperty().divide(8));
dashedArc.setStartAngle(0);
dashedArc.setLength(180);
dashedArc.setType(ArcType.OPEN);
dashedArc.setFill(Color.WHITE);
dashedArc.setStroke(Color.BLACK);
dashedArc.getStrokeDashArray().addAll(6.0, 21.0);
//Create Vertical Lines for the sides
Line leftLine = new Line();
leftLine.startXProperty().bind(ellipse.centerXProperty().subtract(ellipse.radiusXProperty()));
leftLine.startYProperty().bind(ellipse.centerYProperty());
leftLine.endXProperty().bind(solidArc.centerXProperty().subtract(solidArc.radiusXProperty()));
leftLine.endYProperty().bind(solidArc.centerYProperty());
leftLine.setStroke(Color.BLACK);
Line rightLine = new Line();
rightLine.startXProperty().bind(ellipse.centerXProperty().add(ellipse.radiusXProperty()));
rightLine.startYProperty().bind(ellipse.centerYProperty());
rightLine.endXProperty().bind(solidArc.centerXProperty().add(solidArc.radiusXProperty()));
rightLine.endYProperty().bind(solidArc.centerYProperty());
rightLine.setStroke(Color.BLACK);
//Test with a text box
Text text = new Text(25, 25, "WHY IS THIS REQUIRED?????");
//Add the objects to the pane
pane.getChildren().add(text); //WHEN I TAKE THIS LINE OUT, THE PROGRAM JUST HANGS.........
pane.getChildren().addAll(ellipse, solidArc, dashedArc, leftLine, rightLine);
//Set Up Stage
primaryStage.setTitle("Ellipse that scales");
primaryStage.setScene(scene);
primaryStage.show();
}
}
这并不是说你的程序挂起了,而是舞台的大小没有调整到任何你可以通过设置这样的高度和宽度来解决的问题
primaryStage.setWidth(200);
primaryStage.setHeight(200);
文本修复它的原因是因为它创建了一个基本大小,当您删除它时,您可以看到它,因为它太小,您无法再看到它
项目也做得很好,我觉得很酷谢谢!现在很好,我明白为什么了。非常感谢!