2015-04-07 44 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