2015-10-04 73 views
0

我試圖將3個功能合併到一個JavaFX類中。我的第一個功能圍繞一個圓圈顯示「WELCOME TO JAVA」。第二個顯示隨機1和0的10x10矩陣。第三個顯示一個笑臉。它們應該在一個窗格中依次顯示。我有第一個和第三個功能,但矩陣一個是把我扔掉。雖然一切都應該在單個窗格中顯示在同一個GUI窗口中(每位教授),但我不知道除了創建gridPane之外,我還能做其他任何事情。它在沒有大小限制的情況下顯示正常,但是它佔用了整個屏幕,而我的其他2個功能不可見。當我添加約束時,它變小,數字不可見。我不知道如何解決這個問題。有人可以幫忙嗎?在單個GUI窗口中將3個JavaFX功能合併爲一個類

 Pane pane = new Pane(); 

     // Create a circle and set its properties 
     Circle circle = new Circle(); 
     circle.setCenterX(100); 
     circle.setCenterY(100); 
     circle.setRadius(50); 
     circle.setStroke(null); 
     circle.setFill(null); 
     pane.getChildren().add(circle); // Add circle to the pane 

     //Display WELCOME TO JAVA with the text forming a circle 
     int i = 0; 
     String phrase = "WELCOME TO JAVA "; 
     double degree = 360/phrase.length(); 
     for (double degrees = 0; i < phrase.length(); i++, degrees += degree) { 
      double pointX = circle.getCenterX() + circle.getRadius() * 
       Math.cos(Math.toRadians(degrees)); 
      double pointY = circle.getCenterY() + circle.getRadius() * 
       Math.sin(Math.toRadians(degrees)); 
      Text letter = new Text(pointX, pointY, phrase.charAt(i) + ""); 
      letter.setFill(Color.BLACK); 
      letter.setFont(Font.font("Times New Roman", FontWeight.BOLD, 20)); 
      letter.setRotate(degrees + 90); 
      pane.getChildren().add(letter); } 

     //Create a 10x10 matrix of 1s and 0s 
     GridPane pane2 = new GridPane(); 
     pane2.setHgap(1); 
     pane2.setVgap(1); 

     Button[][] matrix; 

     int length = 10; 
     int width = 10; 

     ArrayList<TextField> textFields = new ArrayList<>(); 
     for (int y = 0; y < length; y++) { 
      ColumnConstraints colConst = new ColumnConstraints(); 
      colConst.setPercentWidth(10); 
      pane2.getColumnConstraints().add(colConst); 
      for (int x = 0; x < width; x++) { 
       RowConstraints rowConst = new RowConstraints(); 
       rowConst.setPercentHeight(10); 
       pane2.getRowConstraints().add(rowConst); 
       Random rand = new Random(); 
       int random1 = rand.nextInt(2); 
       TextField textf = new TextField(); 
       textf.setText("" + random1); 
       textf.setPrefSize(15, 15); 
       pane2.setRowIndex(textf, x); 
       pane2.setColumnIndex(textf, y); 
       pane2.getChildren().add(textf); 
      }} 

       //Create a smiley face 
       Circle circle2 = new Circle(); 
       circle2.setCenterX(600.0f); 
       circle2.setCenterY(100.0f); 
       circle2.setRadius(50.0f); 
       circle2.setStroke(Color.BLACK); 
       circle2.setFill(null); 

       pane.getChildren().add(circle2); 

       Circle leftInnerEye = new Circle(); 
        leftInnerEye.setCenterX(580.0f); 
        leftInnerEye.setCenterY(85.0f); 
        leftInnerEye.setRadius(5); 
        leftInnerEye.setStroke(Color.BLACK); 
        pane.getChildren().add(leftInnerEye); 

       Ellipse leftOutterEye = new Ellipse(); 
        leftOutterEye.setCenterX(580.0f); 
        leftOutterEye.setCenterY(85.0f); 
        leftOutterEye.setRadiusX(11.0f); 
        leftOutterEye.setRadiusY(8.0f); 
        leftOutterEye.setStroke(Color.BLACK); 
        leftOutterEye.setFill(null); 
        pane.getChildren().add(leftOutterEye); 

       Circle rightEye = new Circle(); 
        rightEye.setCenterX(620.0f); 
        rightEye.setCenterY(85.0f); 
        rightEye.setRadius(5); 
        rightEye.setStroke(Color.BLACK); 
        pane.getChildren().add(rightEye); 

       Ellipse rightOutterEye = new Ellipse(); 
        rightOutterEye.setCenterX(620.0f); 
        rightOutterEye.setCenterY(85.0f); 
        rightOutterEye.setRadiusX(11.0f); 
        rightOutterEye.setRadiusY(8.0f); 
        rightOutterEye.setStroke(Color.BLACK); 
        rightOutterEye.setFill(null); 
        pane.getChildren().add(rightOutterEye); 

       Polygon nose = new Polygon(); 
        nose.getPoints().setAll(
          600d, 90d, 
          588d, 115d, 
          612d, 115d); 
        nose.setStroke(Color.BLACK); 
        nose.setFill(null); 
        pane.getChildren().add(nose); 

       Arc mouth = new Arc(600, 115, 30, 16, 180, 180); 
        mouth.setFill(null); 
        mouth.setType(ArcType.OPEN); 
        mouth.setStroke(Color.BLACK); 
        pane.getChildren().add(mouth); 

      HBox hbox = new HBox(pane, pane2); 
      hbox.autosize(); 
      hbox.setAlignment(Pos.BASELINE_LEFT); 
      hbox.setPadding(new Insets(20)); 

     // Create a scene and place it in the stage 
     Scene scene = new Scene(hbox, 1000, 500); 
     primaryStage.setTitle("Laura's Chapter 14"); // Set the stage title 
     primaryStage.setScene(scene); // Place the scene in the stage 
     primaryStage.show(); // Display the stage 
     } 

    catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    launch(args); 
} 
+0

我剛剛從教授那裏聽到說明書不正確。它不應該是單個窗格。它應該是單獨的。所以我唯一需要幫助的是正確調整矩陣窗格的大小。 –

+0

嗨,您可能會考慮編輯您的問題以反映您的新要求,以便我們可以更好地爲您提供幫助。你可能也想盡可能地壓縮這個問題,因爲它很廣泛。你的代碼的關鍵是要麼給你帶來麻煩,要麼你不明白? – eeeeeean

回答

0

創建各功能的窗格,然後添加功能,無論是VBoxHBox,將根現場的窗格。這樣你可以有一個GridPane你的第二個功能。 JavaFX中有不同的佈局,它們的行爲有所不同。看看這documentation及其子文件。