2016-09-29 50 views
-1

我是javafx的新手,並且正在嘗試製作一個類似於飛鏢的具有不同大小/顏色橢圓環的GUI。最終目標是使它成爲一個動畫並讓它旋轉,但現在我只是試圖在添加動畫之前讓GUI看起來/正常工作。我的問題是,當我運行該程序時,GUI出現空白/空白。我不知道爲什麼所以如果你們看到我不知道的事情,並且對新手有任何提示,請通過任何方式告訴我!以下是我迄今爲止編寫的代碼:javafx 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

你是否試圖縮小它的範圍? –

+0

@SomePerson我已經修正了錯誤,現在我需要讓它做動畫。我想戒指跳動和跳舞:P – IhateJava

回答

1

您正在將for循環中的ringNum初始化爲-1,並且只要它是>=0即可迭代。即你根本沒有迭代。同樣,您將rings數組初始化爲一個零項數組。

想必你實際上想把numRings設置爲0以外的值。同樣,您可能想要將其他變量的值初始化爲合理值。

+0

我改變了numRings = 20;因爲我想在GUI中使用20個環,但我仍然沒有得到任何內容的GUI :(@james_D – IhateJava

+0

也許製作零寬度和高度的橢圓不會創建任何可以看到的東西... –

+0

哦哇hahahahah我現在知道了:D哇,這是有史以來最明顯的事情洛洛爾 – IhateJava