I am new to javafx and am trying to make a GUI that has rings of ellipses of different sizes/colors similar to a dart board. the end goal is to make it an animation and have it spin and such but for now I am just trying to get the GUI to look/work properly before I add animation. My issue is when I run the program the GUI comes up blank/empty. I'm not sure as of why so if you guys see something that I don't and have any tips to a rookie please by all means let me know!here is the code I have written so far:
我是javafx的新手,我正在尝试制作一个具有不同尺寸/颜色的椭圆环的GUI,类似于飞镖板。最终的目标是使它成为一个动画并让它旋转,但是现在我只是想在我添加动画之前让GUI看起来/正常工作。我的问题是当我运行程序时,GUI出现空白/空白。我不确定为什么如此,如果你们看到我没有的东西,并且对新手有任何提示,请务必告诉我!这是我到目前为止编写的代码:
package targetpractice;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class targetpractice extends Application {
private Ellipse[] rings;
private Color[] colors = {Color.BLUE, Color.RED, Color.YELLOW};
private final int NUMCOLORS = 3;
double width = 0;
double height = 0;
int numRings = 0;
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
rings = new Ellipse[numRings];
for (int ringNum = numRings - 1; ringNum >= 0; ringNum--) {
Ellipse e = new Ellipse(width / 2, height / 2, ringNum * (0.5 * width / numRings), ringNum * (0.5 * height / numRings));
e.setFill(colors[ringNum % NUMCOLORS]);
e.setStroke(Color.BLACK);
rings[ringNum] = e;
pane.getChildren().add(e);
}
Scene scene = new Scene(pane, 600, 400);
primaryStage.setTitle("target practice");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
1 个解决方案
#1
1
You are initializing ringNum
in the for
loop to -1
, and iterating as long as it is >=0
. I.e. you are not iterating at all. Similarly, you initialize the rings
array to an array of zero items.
您正在将for循环中的ringNum初始化为-1,并且只要它> = 0就进行迭代。即你根本没有迭代。同样,您将rings数组初始化为零项目数组。
Presumably you actually wanted to set numRings
to something other than zero. Similarly, you probably want to initialize the values of other variables to something sensible as well.
大概你真的想把numRings设置为零以外的东西。同样,您可能希望将其他变量的值初始化为合理的值。
#1
1
You are initializing ringNum
in the for
loop to -1
, and iterating as long as it is >=0
. I.e. you are not iterating at all. Similarly, you initialize the rings
array to an array of zero items.
您正在将for循环中的ringNum初始化为-1,并且只要它> = 0就进行迭代。即你根本没有迭代。同样,您将rings数组初始化为零项目数组。
Presumably you actually wanted to set numRings
to something other than zero. Similarly, you probably want to initialize the values of other variables to something sensible as well.
大概你真的想把numRings设置为零以外的东西。同样,您可能希望将其他变量的值初始化为合理的值。