【问题标题】:Generating a receipt in JavaFX?在 JavaFX 中生成收据?
【发布时间】:2015-04-07 12:40:11
【问题描述】:

我想知道我的方法是否正确。我正在尝试在生成收据时显示收据(您也可以将其视为动态文本)。我只能想到使用“标签”进行显示。有没有更好的办法?另外,当添加的文本超出标签大小时,它应该变成“可滚动的”。我尝试使用“ScrollPane”,但我的文本只是没有 scollbar“激活”。我只能找到“可滚动”的“图像”而不是“标签”或“文本区域”。欢迎任何帮助或建议。

PS:我刚开始通过试用此应用程序来学习 JavaFX 8,如果不处理此问题,我将无法继续。

【问题讨论】:

  • JavaFX resize text with window 的可能重复项
  • 在滚动窗格中放置标签可能是正确的方法。如果没有看到您的代码,就不可能说出为什么它不适合您;您可以编辑问题以显示您尝试过的内容吗?
  • 我的滚动窗格开始工作了。这是代码: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 您可以将您的评论作为答案并接受它!

标签: text javafx


【解决方案1】:

我建议您为收据制作一个样式漂亮的 html 模板,并使用具有唯一 ID 的跨度。

然后使用jsoup 将您的标签文本放在该范围内,并在网络视图中显示该html。

另一个好处是您可以使用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();
}
}

【讨论】:

  • 当您可以在 ScrollPane 中使用 LabelText 时,这似乎是一种非常重量级的方法。
  • 我同意@James_D。您的解决方案似乎有点繁重,虽然还可以。
猜你喜欢
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 2018-05-04
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多