2015-09-14 76 views
0

對於我正在處理的程序,我決定創建自己的「命令提示符」,這意味着將通常發送到控制檯的錯誤消息發送給它進行調試。之前,當唯一的功能是文本顯示,一切正常。但是,現在,在將根設爲VBox而不是ScrollPane後,格式和佈局很奇怪。下面是控制檯的當前版本,它不工作,以及以前的版本,它工作正常。

舊:VBox內容不填充窗口

import java.sql.SQLException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.scene.text.FontWeight; 
import javafx.stage.Screen; 
import javafx.stage.Stage; 

public class DebugConsoleOld extends Application { 

    private Stage stage; 
    private SimpleDateFormat dateFormat; 

    public void start(final Stage stage) { 
     this.stage = stage; 

     ScrollPane scrollPane = new ScrollPane(); 
     VBox box = new VBox(); 
     Scene scene = new Scene(scrollPane, Screen.getPrimary() 
       .getVisualBounds().getWidth() - 10, Screen.getPrimary() 
       .getVisualBounds().getHeight() - 10); 

     scrollPane.setStyle("-fx-background-color: black;"); 
     box.setStyle("-fx-background-color: black;"); 
     box.setAlignment(Pos.BOTTOM_LEFT); 

     scrollPane.setContent(box); 

     scrollPane.setFitToHeight(true); 
     scrollPane.setFitToWidth(true); 

     stage.setScene(scene); 
     stage.setTitle("Debug Console"); 
     stage.show(); 

     dateFormat = new SimpleDateFormat("[yyyy-M-d HH:mm:ss] "); 

     write(new Exception(), Priority.MEDIUM); 
     write(new NullPointerException(), Priority.MEDIUM); 
     write(new SQLException(), Priority.MEDIUM); 
    } 

    public void write(String message) { 
     message = dateFormat.format(Calendar.getInstance().getTime()) + message; 
     ((VBox) ((ScrollPane) stage.getScene().getRoot()).getContent()) 
      .getChildren().add(textBuilder(message)); 
    } 

    public void write(String message, Priority priority) { 
     write(message, priority.color); 
    } 

    private void write(String message, Color fontColor) { 
     message = dateFormat.format(Calendar.getInstance().getTime()) + message; 
     ((VBox) ((ScrollPane) stage.getScene().getRoot()).getContent()) 
      .getChildren().add(
        textBuilder(message, null, null, 0, fontColor)); 
    } 

    public void write(Exception exception, Priority priority) { 
     // exception.printStackTrace(); 
     write("--- Beginning of Exception ---", Priority.HIGH); 
     write(exception.toString(), priority); 
     StackTraceElement[] arrayOfStackTraceElement = exception 
       .getStackTrace(); 
     Object localObject2; 
     for (StackTraceElement ste : arrayOfStackTraceElement) 
      write("\t at " + ste, priority); 
     localObject2 = exception.getCause(); 
     if (localObject2 != null) 
      write("Caused by: " + localObject2); 
     write("--- End of Exception ---", Priority.HIGH); 
    } 

    private HBox textBuilder(String text) { 
     return textBuilder(text, null, null, 0, null); 
    } 

    @SuppressWarnings("deprecation") 
    private HBox textBuilder(String text, FontWeight fontWeight, 
      FontPosture fontPosture, double fontSize, Color fontColor) { 
     return javafx.scene.layout.HBoxBuilder 
       .create() 
       .children(javafx.scene.control.CheckBoxBuilder.create().build(), 
        javafx.scene.text.TextBuilder 
          .create() 
          .text(text) 
          .font(Font 
            .font("Consolas", 
              (fontWeight == null ? FontWeight.NORMAL 
                : fontWeight), 
               (fontPosture == null ? FontPosture.REGULAR 
                 : fontPosture), 
               (fontSize <= 0 ? 13 : fontSize))) 
           .wrappingWidth(
             ((ScrollPane) stage.getScene() 
               .getRoot()).getWidth() - 2) 
           .fill((fontColor == null ? Color.WHITE 
             : fontColor)).build()).build(); 
    } 

    public int clear() { 
     int items = ((VBox) ((ScrollPane) ((VBox) stage.getScene().getRoot()) 
       .getChildren().get(0)).getContent()).getChildren().size(); 
     ((VBox) ((ScrollPane) ((VBox) stage.getScene().getRoot()).getChildren() 
       .get(0)).getContent()).getChildren().clear(); 
     return items; 
    } 

    public static enum Priority { 
     /** 
     * A message of no great importance 
      */ 
     LOW(Color.LIGHTGRAY), 
     /** 
     * A message of standard importance, or something not necessary to be 
     * noticed 
     */ 
     NORMAL(Color.WHITE), 
     /** 
     * A message of standard importance, but may need developer attention 
     */ 
     MILD(Color.YELLOW), 
     /** 
     * A message needing attention, as something may have broken 
     */ 
     MEDIUM(Color.ORANGE), 
     /** 
     * A message of great importance, something broke, an exception was 
     * thrown, ect. 
     */ 
     HIGH(Color.RED); 

     private Color color; 

     private Priority(Color color) { 
      this.color = color; 
     } 

    } 

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

} 

電流:

import java.text.SimpleDateFormat; 
import java.util.Calendar; 

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Background; 
import javafx.scene.layout.BackgroundFill; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontPosture; 
import javafx.scene.text.FontWeight; 
import javafx.stage.Stage; 

public class DebugConsoleNew extends Application { 

    private Stage stage; 
    private SimpleDateFormat dateFormat; 

    public void start(final Stage stage) { 
     this.stage = stage; 

     ScrollPane scrollPane = new ScrollPane(); 
     VBox pane = new VBox(); 
     VBox box = new VBox(); 
     Scene scene = new Scene(pane, 500, 300); 
     final TextField commandField = new TextField(); 

     scrollPane.setStyle("-fx-background-color: black;"); 
     box.setStyle("-fx-background-color: black;"); 
     box.setAlignment(Pos.BOTTOM_LEFT); 

     scrollPane.setContent(box); 
     commandField.setBackground(new Background(new BackgroundFill(
       Color.BLACK, null, null))); 
     commandField.setStyle("-fx-text-fill: white;"); 

     pane.getChildren().addAll(scrollPane, commandField); 

     scrollPane.setFitToHeight(true); 
     scrollPane.setFitToWidth(true); 

     stage.setScene(scene); 
     stage.setTitle("Debug Console"); 
     stage.show(); 

     dateFormat = new SimpleDateFormat("[yyyy-M-d HH:mm:ss] "); 
     commandField.requestFocus(); 
    } 

    public void write(String message) { 
     message = dateFormat.format(Calendar.getInstance().getTime()) + message; 
     ((VBox) ((ScrollPane) ((VBox) stage.getScene().getRoot()).getChildren() 
      .get(0)).getContent()).getChildren().add(textBuilder(message)); 
    } 

    public void write(String message, Priority priority) { 
     write(message, priority.color); 
    } 

    private void write(String message, Color fontColor) { 
     message = dateFormat.format(Calendar.getInstance().getTime()) + message; 
     ((VBox) ((ScrollPane) ((VBox) stage.getScene().getRoot()).getChildren() 
      .get(0)).getContent()).getChildren().add(
      textBuilder(message, null, null, 0, fontColor)); 
    } 

    public void write(Exception exception, Priority priority) { 
     // exception.printStackTrace(); 
     write("--- Beginning of Exception ---", Priority.HIGH); 
     write(exception.toString(), priority); 
     StackTraceElement[] arrayOfStackTraceElement = exception 
       .getStackTrace(); 
     Object localObject2; 
     for (StackTraceElement ste : arrayOfStackTraceElement) 
      write("\t at " + ste, priority); 
     localObject2 = exception.getCause(); 
     if (localObject2 != null) 
      write("Caused by: " + localObject2); 
     write("--- End of Exception ---", Priority.HIGH); 
    } 

    private HBox textBuilder(String text) { 
     return textBuilder(text, null, null, 0, null); 
    } 

    @SuppressWarnings("deprecation") 
    private HBox textBuilder(String text, FontWeight fontWeight, 
      FontPosture fontPosture, double fontSize, Color fontColor) { 
     return javafx.scene.layout.HBoxBuilder 
       .create() 
       .children(
         javafx.scene.control.CheckBoxBuilder.create().build(), 
         javafx.scene.text.TextBuilder 
           .create() 
           .text(text) 
           .font(Font 
             .font("Consolas", 
               (fontWeight == null ? FontWeight.NORMAL 
                 : fontWeight), 
               (fontPosture == null ? FontPosture.REGULAR 
                 : fontPosture), 
               (fontSize <= 0 ? 13 : fontSize)))  
           .wrappingWidth(
             ((VBox) stage.getScene().getRoot()) 
               .getWidth() - 2) 
           .fill((fontColor == null ? Color.WHITE 
             : fontColor)).build()).build(); 
    } 

    public int clear() { 
     int items = ((VBox) ((ScrollPane) ((VBox) stage.getScene().getRoot()) 
       .getChildren().get(0)).getContent()).getChildren().size(); 
    ((VBox) ((ScrollPane) ((VBox) stage.getScene().getRoot()).getChildren() 
       .get(0)).getContent()).getChildren().clear(); 
     return items; 
    } 

    public static enum Priority { 
     /** 
     * A message of no great importance 
     */ 
     LOW(Color.LIGHTGRAY), 
     /** 
     * A message of standard importance, or something not necessary to be 
     * noticed 
     */ 
     NORMAL(Color.WHITE), 
     /** 
     * A message of standard importance, but may need developer attention 
     */ 
     MILD(Color.YELLOW), 
     /** 
     * A message needing attention, as something may have broken 
     */ 
     MEDIUM(Color.ORANGE), 
     /** 
     * A message of great importance, something broke, an exception was 
     * thrown, ect. 
     */ 
     HIGH(Color.RED); 

     private Color color; 

     private Priority(Color color) { 
      this.color = color; 
     } 

    } 

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

} 


上面的代碼能夠被複制到一個IDE和執行。有沒有人有任何想法,爲什麼格式得到所有wonky?

+1

發表[MCVE]不是鏈接到其他網站上 – Reimeus

+0

代碼@Reimeus更新,其中的主要問題代碼更緊湊的版本。但是,上面列出的所有內容都需要編譯/運行成功。 – ZonalYewHD

+1

不相關的,但建設者已棄用,你應該重構你的代碼不要使用它們。此外,您可能會考慮使用FXML並將對您的控件的引用注入到FXML控制器中,而不是使用真正深度的getChildren語句和強制轉換。 – jewelsea

回答

1

這真的很難理解你的代碼試圖做的,因爲所有的怪鑄件等不調用一個ScrollPanesetFitToWidth(true)setFitToHeight(true)的使ScrollPane多餘?

如果目的是讓ScrollPane使用提供給VBox任何額外的空間,你可以叫

VBox.setVgrow(scrollPane, javafx.scene.layout.Priority.ALWAYS); 
+0

要重申@jewelsea在上述評論中提到的內容,您需要刪除[已棄用]的構建器類(http://mail.openjdk.java.net/pipermail/openjfx-dev/2013-March/ 006725.html),並將在未來的版本中完全刪除(可能只要Java 9)。 –