这是我的代码:
public class Test extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
var textFlow1 = createTextFlow();
textFlow1.setStyle("-fx-background-color: cyan");
var textFlow2 = createTextFlow();
textFlow2.setStyle("-fx-background-color: yellow");
VBox vbox = new VBox(textFlow1, textFlow2);
Scene scene = new Scene(vbox, 300, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
private TextFlow createTextFlow() {
Text textA = new Text("ABC");
textA.setStyle("-fx-font-family: 'monospace'; -fx-font-size: 14;");
Line line = new Line(0, 0, 0, 20);
Text textB = new Text("DEF");
textB.setStyle("-fx-font-family: 'monospace'; -fx-font-size: 14;");
TextFlow textFlow = new TextFlow(textA, line, textB);
return textFlow;
}
public static void main(String[] args) {
launch(args);
}
}
结果如下:
如您所见,我有两个 TextFlow,每个都有一个行节点。我需要这两行看起来像一行,换句话说,它们之间不能有空格(现在我有大约 4px 的间隙)
有人能说说如何连接这些线(如何使它们看起来像一条线)吗?
您的问题可能需要更多背景和解释。(例如,您为什么要这样做?为什么选择
20
线的高度?)如果没有这些,很难知道这个解决方案是否能从您发布的示例推广到您正在尝试解决的实际问题。也就是说,以下内容将实现您特别要求的内容。这个想法是通过设置为来不让管理的定位
TextFlow
,Line
或Line
影响的大小计算。然后覆盖以手动定位行。您需要始终将设置为文本流的高度,并将和设置为(在此特定示例中)第二个元素的 x 位置。TextFlow
managed
false
TextFlow.layoutChildren()
startY
0
endY
startX
endX
Text