对于我正在处理的程序,我决定创建自己的"命令提示符",这意味着通常发送到控制台的错误消息将发送给它进行调试。之前,当唯一的功能是显示文本时,一切正常。但是,现在,在我创建了VBox
而不是ScrollPane
之后,格式和布局很奇怪。下面是当前版本的控制台,它不起作用,以及之前的版本,它工作正常
旧:
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中并执行。有没有人知道格式化为什么会变得难以理解?
答案 0 :(得分:1)
很难理解你的代码尝试做什么,因为所有奇怪的演员表等都没有setFitToWidth(true)
和setFitToHeight(true)
同时调用{{} 1}}使ScrollPane
多余?
如果目的是让ScrollPane
使用ScrollPane
可用的任何额外空间,您可以致电
VBox