Показаны различия между двумя версиями страницы.
Предыдущая версия справа и слева Предыдущая версия Следующая версия | Предыдущая версия | ||
develop:java:javafx_howto [2023/06/26 14:11] admin |
develop:java:javafx_howto [2023/08/05 11:49] (текущий) admin |
||
---|---|---|---|
Строка 3: | Строка 3: | ||
===== Сборка/ | ===== Сборка/ | ||
+ | < | ||
+ | < | ||
т.к. пакет FX выпилили в отдельный модуль, | т.к. пакет FX выпилили в отдельный модуль, | ||
Вроде как можно с помощью мавена включить зависимости в состав пакета, | Вроде как можно с помощью мавена включить зависимости в состав пакета, | ||
Строка 9: | Строка 11: | ||
java --module-path path/ | java --module-path path/ | ||
</ | </ | ||
+ | </ | ||
< | < | ||
Строка 120: | Строка 123: | ||
com.itextpdf.text.Document document = new com.itextpdf.text.Document(); | com.itextpdf.text.Document document = new com.itextpdf.text.Document(); | ||
PdfWriter pdfWriter = PdfWriter.getInstance(document, | PdfWriter pdfWriter = PdfWriter.getInstance(document, | ||
- | |||
document.setPageSize(new com.itextpdf.text.Rectangle(58, | document.setPageSize(new com.itextpdf.text.Rectangle(58, | ||
document.setMargins(2, | document.setMargins(2, | ||
document.open(); | document.open(); | ||
+ | |||
PdfContentByte pdfContentByte = pdfWriter.getDirectContent(); | PdfContentByte pdfContentByte = pdfWriter.getDirectContent(); | ||
+ | |||
+ | // Генерируем баркод | ||
Barcode128 barcode128 = new Barcode128(); | Barcode128 barcode128 = new Barcode128(); | ||
barcode128.setCode(" | barcode128.setCode(" | ||
barcode128.setCodeType(Barcode128.CODE128); | barcode128.setCodeType(Barcode128.CODE128); | ||
+ | |||
+ | // Размеры | ||
+ | barcode128.setSize(10); | ||
+ | barcode128.setBarHeight(70); | ||
+ | |||
com.itextpdf.text.Image code128Image = barcode128.createImageWithBarcode(pdfContentByte, | com.itextpdf.text.Image code128Image = barcode128.createImageWithBarcode(pdfContentByte, | ||
code128Image.scaleToFit(55, | code128Image.scaleToFit(55, | ||
- | code128Image.setAlignment(com.itextpdf.text.Image.ALIGN_MIDDLE); | + | code128Image.setAbsolutePosition(10,10); |
document.add(code128Image); | document.add(code128Image); | ||
document.close(); | document.close(); | ||
Строка 149: | Строка 158: | ||
- | + | ===== Печать | |
- | + | ||
- | ===== ===== | + | |
< | < | ||
- | < | + | < |
- | < | + | |
+ | < | ||
+ | java.awt.Image awtImage = barcode128.createAwtImage(Color.black, | ||
+ | PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, | ||
+ | PrinterJob job = PrinterJob.getPrinterJob(); | ||
+ | // Размеры страницы для печати | ||
+ | PageFormat pageForm = job.defaultPage(); | ||
+ | Paper paper = pageForm.getPaper(); | ||
+ | double width = 8d * 72d; | ||
+ | double height = 4d * 72d; | ||
+ | double margin = 1d * 72d; | ||
+ | paper.setSize(width, | ||
+ | // | ||
+ | paper.setImageableArea(0, | ||
+ | // | ||
+ | pageForm.setPaper(paper); | ||
+ | job.setPrintable(new Printable() { | ||
+ | public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { | ||
+ | if (pageIndex != 0) { | ||
+ | return NO_SUCH_PAGE; | ||
+ | } | ||
+ | Graphics2D g2d = (Graphics2D) graphics; | ||
+ | g2d.drawImage(awtImage, | ||
+ | // Пишем текст | ||
+ | g2d.setFont(new Font(Font.SANS_SERIF, | ||
+ | FontMetrics fm = g2d.getFontMetrics(); | ||
+ | String text = selectedProd.getName(); | ||
+ | // центрирование текста под ШК | ||
+ | int centerBarcode = awtImage.getWidth(null) / 2 + 40; | ||
+ | int centerText = fm.stringWidth(text) / 2; | ||
+ | // добавляем начальный отступ, | ||
+ | // добвялем высоту самого изображения и высоту текста | ||
+ | int yForText= 5 + awtImage.getHeight(null) + fm.getAscent(); | ||
+ | g2d.drawString(text, | ||
+ | // объем на второй строке | ||
+ | text = selectedProd.getSelectedVolume().getName(); | ||
+ | // центрирование текста под ШК | ||
+ | centerText = fm.stringWidth(text) / 2; | ||
+ | // добавляем начальный отступ, | ||
+ | // добвялем высоту самого изображения и высоту текста | ||
+ | yForText= 5 + awtImage.getHeight(null) + (fm.getAscent() * 2); | ||
+ | g2d.drawString(text, | ||
+ | return PAGE_EXISTS; | ||
+ | } | ||
+ | }, pageForm); | ||
+ | job.setPrintService(printServices[0]); | ||
+ | //if (job.printDialog()) | ||
+ | job.print(); | ||
</ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Сообщения ===== | ||
+ | |||
+ | < | ||
+ | < | ||
<code bash> | <code bash> | ||
+ | import javafx.scene.control.Alert; | ||
+ | ... | ||
+ | Alert alert = new Alert(Alert.AlertType.INFORMATION); | ||
+ | |||
+ | alert.setTitle(" | ||
+ | alert.setHeaderText(null); | ||
+ | alert.setContentText(" | ||
+ | |||
+ | alert.showAndWait(); | ||
</ | </ | ||
</ | </ | ||
- | |||