2017-09-13 100 views
0

我試圖使一行文本由一個名稱和一個文本字符串組成。我想要這個名字是一個超鏈接,剩下的只是純文本。在JavaFX中禁用TextFlow中的項目之間的間距

我認爲TextFlow會很好,但問題是它會自動在超鏈接和文本之間放置一個空格。如果我想TextFlow的是例如

Jane的真棒

的TextFlow將一個

Jane的真棒

有一個方法或CSS屬性來禁用此行爲?

回答

2

解決方案

您可以通過CSS樣式移除填充:

.hyperlink { 
    -fx-padding: 0; 
} 

或者,如果你願意,你可以做到這一點的代碼:

link.setPadding(new Insets(0)); 

背景

默認設置可以在modena.css文件中jfxrt.jar文件與JRE分發包裝中找到,它是:

-fx-padding: 0.166667em 0.25em 0.166667em 0.25em; /* 2 3 2 3 */ 

示例應用程序

sample application

在樣本截圖第二超鏈接有焦點(因此它的虛線邊框)。

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Hyperlink; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Text; 
import javafx.scene.text.TextFlow; 
import javafx.stage.Stage; 

public class HyperSpace extends Application { 

    @Override 
    public void start(Stage stage) { 
     TextFlow textFlow = new TextFlow(
      unstyle(new Hyperlink("Jane")), 
      new Text("'s awesome "), 
      unstyle(new Hyperlink("links")) 
     ); 
     stage.setScene(new Scene(new Pane(textFlow))); 
     stage.show(); 
    } 

    private Hyperlink unstyle(Hyperlink link) { 
     link.setPadding(new Insets(0)); 
     return link; 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
相關問題