gpt4 book ai didi

java - VBox 内容未填充窗口

转载 作者:行者123 更新时间:2023-12-01 11:09:09 31 4
gpt4 key购买 nike

对于我正在开发的程序,我决定创建自己的“命令提示符”,这意味着通常发送到控制台的错误消息将发送到它进行调试。以前,当唯一的功能是显示文本时,一切都工作正常。然而现在,当我将根设为 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中并执行。有谁知道为什么格式变得很奇怪吗?

最佳答案

由于所有奇怪的强制转换等,很难理解您的代码想要做什么。不同时调用 setFitToWidth(true)setFitToHeight(true)ScrollPane 上使 ScrollPane 变得多余?

如果目的是让 ScrollPane 使用 VBox 可用的任何额外空间,您可以调用

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

关于java - VBox 内容未填充窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575010/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com