在 JavaFX 中生成收据?
Generating a receipt in JavaFX?
我想知道我的做法是否正确。我正在尝试显示正在生成的收据(您也可以将其视为动态文本)。我只能想到使用 'Label' 来显示。有没有更好的办法?另外,当添加的文本超出标签大小时,它应该变成 "scrollable"。我尝试使用 'ScrollPane',但我的文本只是没有 scollbar "activating"。我只能找到“Image's being made "scrollable" 而不是 'Label's 或 'TextArea's。欢迎任何帮助或建议。
PS:我刚刚通过试用此应用程序开始学习 JavaFX 8,如果不处理此应用程序我将无法继续。
我建议您制作一个 html 模板,为您的收据设计漂亮的样式,并使用具有唯一 ID 的 span。
然后使用 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();
}
}
我想知道我的做法是否正确。我正在尝试显示正在生成的收据(您也可以将其视为动态文本)。我只能想到使用 'Label' 来显示。有没有更好的办法?另外,当添加的文本超出标签大小时,它应该变成 "scrollable"。我尝试使用 'ScrollPane',但我的文本只是没有 scollbar "activating"。我只能找到“Image's being made "scrollable" 而不是 'Label's 或 'TextArea's。欢迎任何帮助或建议。
PS:我刚刚通过试用此应用程序开始学习 JavaFX 8,如果不处理此应用程序我将无法继续。
我建议您制作一个 html 模板,为您的收据设计漂亮的样式,并使用具有唯一 ID 的 span。
然后使用 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();
}
}