JFrame:打印不使用malayalam文本

时间:2013-12-05 17:00:19

标签: java swing

我编写了一个打印JFrame内容的Java程序。它在框架中的标签中与英语完美配合,但当我在其中使用马拉雅拉姆语unicode字符串时出错。

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame; 
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JLabel;

public class MalayalamPrintingSSCCE {

private static Frame frame;

public static void main(String[] args) {

    frame = new Frame();
    frame.setLayout(new FlowLayout());
    frame.add(new JLabel("Vishnu1"));
    frame.add(new JLabel("\u0D35\u0D3F\u0D37\u0D4D\u0D23\u0D41"));//വിഷ്ണു
    frame.add(new JLabel("\u0D35\u0D3F\u0D37\u0D4D\u0D23\u0D41"));//വിഷ്ണു
    frame.add(new JLabel("\u0D35\u0D3F\u0D37\u0D4D\u0D23\u0D41"));//വിഷ്ണു
    frame.add(new JLabel("Vishnu4"));
    frame.setVisible(true);
    frame.pack();

    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setJobName(" Print Component ");

    pj.setPrintable(new Printable() {
        public int print(Graphics pg, PageFormat pf, int pageNum) {
            if (pageNum > 0) {
                return Printable.NO_SUCH_PAGE;
            }
            Dimension dim = frame.getSize();
            double cHeight = dim.getHeight();
            double cWidth = dim.getWidth();

            // get the bounds of the printable area
            double pHeight = pf.getImageableHeight();
            double pWidth = pf.getImageableWidth();

            double pXStart = pf.getImageableX();
            double pYStart = pf.getImageableY();

            double xRatio = pWidth / cWidth;
            double yRatio = pHeight / cHeight;

            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pXStart, pYStart);
            g2.scale(xRatio, xRatio);
            frame.paint(g2);
            return Printable.PAGE_EXISTS;
            }
        });

        try {
            if (pj.printDialog()) {
                    pj.print();
                }
            } catch (PrinterException ex) {
            // handle exception
            }
        }
    }

英文打印机输出: Print output with english 使用马拉雅拉姆语输出打印机 print output with malayalam 但是,框架渲染正确如下: enter image description here 我怎样才能解决这个问题?有没有更好的方法呢?

1 个答案:

答案 0 :(得分:5)

您可以将font添加到label,然后在Frame

上打印马拉雅拉姆语

以下显示了一个在malayalam中打印我姓名的简单代码:

import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;

    /**
     * @author Rakesh KR
     */

public class SimpleGUI1 {



    public static void main(String[] args) {
        SimpleGUI1 gUI = new SimpleGUI1();
        gUI.action();
    }

    void action(){
        JFrame  frame  = new JFrame();
        JLabel label = new JLabel("രാകേഷ്");
        // Can also use unicode 
        //JLabel label= new JLabel("\u0d30\u0d3e\u0d15\u0d47\u0d37\u0d4d");
        Font font = new Font("Arial Unicode MS", Font.PLAIN, 12);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label.setFont(font);
        frame.add(label);
        frame.setSize(100,100);
        frame.setVisible(true);

    }


}

输出

enter image description here

修改

通过使用上面frame(添加的字体),我们还可以打印马拉雅拉姆语。

    import java.awt.Font;
    import javax.swing.JFrame;
    import javax.swing.JLabel;

        /**
         * @author Rakesh KR
         */

    public class SimpleGUI1 {



        public static void main(String[] args) {
            SimpleGUI1 gUI = new SimpleGUI1();
            gUI.action();
        }

        void action(){
            JFrame  frame  = new JFrame();
            JLabel label = new JLabel("രാകേഷ്");
            // Can also use unicode 
            //JLabel label= new JLabel("\u0d30\u0d3e\u0d15\u0d47\u0d37\u0d4d");
            Font font = new Font("Arial Unicode MS", Font.PLAIN, 12);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            label.setFont(font);
            frame.add(label);
            frame.setSize(100,100);
            frame.setVisible(true);
            PrinterJob pj = PrinterJob.getPrinterJob();
            pj.setJobName(" Print Component ");

            pj.setPrintable(new Printable() {
                public int print(Graphics pg, PageFormat pf, int pageNum) {
                    if (pageNum > 0) {
                        return Printable.NO_SUCH_PAGE;
                    }
                    Dimension dim = frame.getSize();
                    double cHeight = dim.getHeight();
                    double cWidth = dim.getWidth();

                    // get the bounds of the printable area
                    double pHeight = pf.getImageableHeight();
                    double pWidth = pf.getImageableWidth();

                    double pXStart = pf.getImageableX();
                    double pYStart = pf.getImageableY();

                    double xRatio = pWidth / cWidth;
                    double yRatio = pHeight / cHeight;

                    Graphics2D g2 = (Graphics2D) pg;
                    g2.translate(pXStart, pYStart);
                    g2.scale(xRatio, xRatio);
                    frame.paint(g2);
                    return Printable.PAGE_EXISTS;
                    }
                });

                try {
                    if (pj.printDialog()) {
                            pj.print();
                        }
                    } catch (PrinterException ex) {

                    }
                }
        }
}

输出: Pdf保存打印页面的格式 Pdf saved format of printing page

单个标签上的

编辑 结果还可以。但是有两种语言的多个标签,就会发生错误 为此,您需要在JPanel中添加JFrame,然后在JPanel添加所需的组件,例如JLabel

        final JFrame  frame  = new JFrame();
        JPanel panel  = new JPanel();
        JLabel label = new JLabel("രാകേഷ്");
        JLabel label1 = new JLabel("Rakesh");
        JLabel label2 = new JLabel("\u0d30\u0d3e\u0d15\u0d47\u0d37\u0d4d");
        JLabel label3 = new JLabel("Rakesh");
        Font font = new Font("Arial Unicode MS", Font.PLAIN, 12);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        label.setFont(font);
        label1.setFont(font);
        label2.setFont(font);
        label3.setFont(font);
        panel.add(label);
        panel.add(label1);
        panel.add(label2);
        panel.add(label3);
        frame.add(panel);
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.pack();

输出:

enter image description here

enter image description here