2015-04-07 45 views
0

我想知道我的方法是否正確。我正在嘗試顯示正在生成的收據(您也可以將其視爲動態文本)。我只能想到使用'標籤'顯示。有沒有更好的辦法?另外,當添加的文本超出標籤大小時,它應該變成「可滾動」。我嘗試使用'ScrollPane',但是我的文本沒有使用scollbar「激活」。我只能找到「圖像被製作成」可滾動「而不是」標籤「或」TextArea「。任何幫助或建議是受歡迎的。在JavaFX中生成收據?

PS:我剛開始學習JavaFX 8通過嘗試這個應用程序,我無法繼續處理這個。

+0

可能重複(即收到http://stackoverflow.com/questions/29315469/javafx-resize-text-with-window) – ItachiUchiha

+0

將標籤放在滾動窗格中可能是正確的方法。沒有看到你的代碼就無法說明爲什麼它不適合你;你可以編輯問題來顯示你的嘗試? –

+0

我讓我的滾動窗格工作。這是代碼:'ScrollPane sp = new ScrollPane(); sp.setContent(BillLabel); sp.setPrefSize(180,200); sp.setPannable(true); sp.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); sp.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);' – Barry66

回答

1

我會建議你做一個很好的造型爲您的收據和使用獨特的身份證跨度的HTML模板。

然後使用jsoup將標籤文本放置在該範圍內並在webview中顯示html。

還有一個好處是,你可以再打印使用[JavaFX的調整與窗口文本] javafx8 webview printing

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.web.WebView; 
import javafx.stage.Stage; 

import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 

public class HtmlReceipt extends Application{ 

String htmlTemplate = "<html>" 
     + "<head>" 
     + "<style>" 
     + "body {background-color: yellow;}" 
     + "#label1 {" 
     + "background-color:red;" 
     + "border:1px solid #000" 
     + "}" 
     + "</style>" 
     + "</head>" 
     + "<body>" 
     + "<span id = 'label1'></span>" 
     + "</body></html>"; 

@Override 
public void start(Stage primaryStage) throws Exception { 
    AnchorPane rootpane = new AnchorPane(); 
    Scene scene = new Scene(rootpane); 
    WebView webView = new WebView(); 
    webView.setPrefHeight(400); 
    webView.setPrefWidth(300); 
    webView.getEngine().loadContent(getReceipt("MyName")); 
    rootpane.getChildren().add(webView); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

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

public String getReceipt(String labelText){ 
    Document doc = Jsoup.parse(htmlTemplate); 
    Element span = doc.select("span#label1").first(); 
    span.text(labelText); 
    return doc.html(); 
} 
} 

enter image description here

+0

這似乎是一個非常重量級的方法,只需在ScrollPane中使用'Label'或'Text'。 –

+0

我同意@James_D。你的解決方案似乎有點沉重,雖然很有意義。 – Barry66