如何打印窗格或整个FXML页面。 我尝试这两种方法(doPrint和doPrintSecond在Controller中)来打印但不起作用; 请纠正我错在哪里。 有没有其他方法来打印fxml页面或窗格。
FXML代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="186.0" prefWidth="457.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication10.PrintPaneController">
<children>
<Pane fx:id="printPane" prefHeight="139.0" prefWidth="457.0" style="-fx-background-color: grey;">
<children>
<Label layoutX="179.0" layoutY="21.0" text="Print">
<font>
<Font size="45.0" />
</font>
</Label>
<Label layoutX="152.0" layoutY="93.0" text="JavaFX print Pane">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutY="139.0" prefHeight="56.0" prefWidth="457.0" style="-fx-background-color: white;">
<children>
<JFXButton buttonType="RAISED" layoutX="192.0" layoutY="14.0" onAction="#printActionPane" text="Print">
<font>
<Font size="20.0" />
</font>
</JFXButton>
</children>
</Pane>
</children>
</AnchorPane>
控制器代码
package javafxapplication10;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.print.PrinterJob;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
public class PrintPaneController implements Initializable {
@FXML
private Pane printPane;
@FXML
private JFXTextField name;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void printActionPane(ActionEvent event) {
//First Method
doPrintSecond(root);
//Second Method
if (doPrint(printPane)) {
System.out.println("Print");
} else {
System.out.println("error");
}
}
//First
void doPrintSecond(Node printPane) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null) {
boolean printed = job.printPage(printPane);
if (printed) {
job.endJob();
} else {
System.out.print("Faled");
}
} else {
System.out.print("could");
}
}
//Second
boolean doPrint(Node printPane) {
PrinterJob job = PrinterJob.createPrinterJob();
if (job == null) {
System.out.println("1");
return false;
}
if (!job.showPrintDialog(null)) {
System.out.println("2");
return false;
}
if (!job.printPage(printPane)) {
System.out.println("3");
return false;
}
return job.endJob();
}
}
This is Application and i click in print button but it not working.
答案 0 :(得分:0)
您可以使用此代码来打印窗格。同样,在 PageLayout 的帮助下,您可以设置所需的纸张大小,其方向,还可以设置边距< / strong>。
我希望这会有所帮助!
Printer printer = Printer.getDefaultPrinter();
PageLayout pageLayout = printer.createPageLayout(Paper.A4,
PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
PrinterJob job = PrinterJob.createPrinterJob();
if (job != null && job.showPrintDialog(printPane.getScene().getWindow())) {
boolean success = job.printPage(pageLayout, printPane);
if (success) {
job.endJob();
}
}