【问题标题】:Print Pane or Whole FXML Page JAVAFX打印窗格或整个 FXML 页面 JAVAFX
【发布时间】:2017-10-23 06:03:52
【问题描述】:

如何打印窗格或整个 FXML 页面。 我尝试这两种方法(doPrint 和 doPrintSecond 在控制器中)打印但不起作用; 请纠正我哪里错了。 有没有其他方法可以打印 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.

【问题讨论】:

    标签: javafx printing fxml


    【解决方案1】:

    您可以使用此代码打印窗格。此外,在 PageLayout 的帮助下,您可以设置所需的 纸张尺寸方向,还可以设置 边距 为您的页面。

    我希望这会有所帮助!

        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();
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      相关资源
      最近更新 更多