如何在同一行java上打印图标和文字?

时间:2014-09-12 23:19:37

标签: java swing

我需要在Java项目中打印纸张上的文本和图标。我的问题是图标和文字不在同一行;图标略高于文字。如果我将图标更改为在同一行上更大。但是我不想更改图标的大小,因为图标的大小将大于文本的大小。有什么办法吗?

我的代码如下:

package PDFAnnotationPackage;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class TestPrint  {  

private static JTextPane pane;
      private static  JFrame frame;
private static StyledDocument styledDocument;
       private static String txt;
public static void main(String[] args) { 
          frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    try {
        print("");
    } catch (PrinterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}


private static  ImageIcon getAnotIconPrint(){
     Image image;           
     ImageIcon icon=null;   
     URL fileName=TestPrint.class.getResource("QuestionMark.gif");      
     icon=(ImageIcon)getIconImage(fileName, 12,12); 
     return icon;

}

private static ImageIcon getIconImage(URL fileName, int h, int w){
    Image image;
    ImageIcon newIcon = null;
     try {
        image = ImageIO.read(fileName);         
        ImageIcon icon = new ImageIcon(fileName); 
        Image img = icon.getImage(); 
        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); 
        Graphics g = bi.createGraphics();
        g.drawImage(img, 0, 0, 15, 15, null);            
         newIcon = new ImageIcon(bi); 
    } catch (IOException e) {            
        e.printStackTrace();
    }

     return newIcon;


}
public static void print (String txt) throws PrinterException{

     // Create the StyleContext, the document and the pane
    pane = new JTextPane();
     Icon icon=getAnotIconPrint();
        //Icon icon=getAnotIconPrint("test");   
        styledDocument = pane.getStyledDocument();              
        Style iconStyle = styledDocument.addStyle("icon", null);
          StyleConstants.setIcon(iconStyle, icon);
          try {
              //insertIconOnDoc(iconStyle);
                   pane.insertIcon(icon);
              insertTextOnDoc(txt);


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

         // pane.print();
                frame.getContentPane().add(pane, BorderLayout.CENTER);           
          frame.setSize(360, 180);
          frame.setVisible(true);

}

    private static void insertTextOnDoc(String content ) throws BadLocationException{
        String txt="  ";        
        txt=txt+"c.author"+"\t" + "Page 2 09/09/2014" ;
        if (content!=null){
            txt=txt+"\n"+content;
        }
        txt=txt+"\n\n";             
        StyleContext sc = new StyleContext();
        Style textStyle = sc.addStyle("TextFromat", null);      
        StyleConstants.setForeground(textStyle, Color.red);
        StyleConstants.setFontSize(textStyle, 12);
        StyleConstants.setFontFamily(textStyle, "serif");
        StyleConstants.setBold(textStyle, true);
        StyleConstants.setLeftIndent(textStyle, 8);
        StyleConstants.setFirstLineIndent(textStyle, 0);        
        styledDocument.insertString(styledDocument.getLength(), txt+" ", textStyle); //must have space for text; otherwise the last line will not print


    }
     private static void insertIconOnDoc(Style style) throws Exception
     {
          if (style.getAttribute(StyleConstants.IconAttribute) != null)
          {
               if (styledDocument.getLength() > 0)
               {
                    Element e = styledDocument.getCharacterElement(styledDocument.getLength() - 1);
                    AttributeSet attributeSet = e.getAttributes();
                    if (style.getAttribute(StyleConstants.IconAttribute) == attributeSet
                              .getAttribute(StyleConstants.IconAttribute))
                    {
                         styledDocument.insertString(styledDocument.getLength(), " ", StyleContext
                                   .getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE));
                    }
               }
          }

         styledDocument.insertString(styledDocument.getLength(),  " ", style);


     }

}

我的代码输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

我通过引用JTextPane Icon and Text来解决这个问题。我在标签中添加了我的图标和文字并将其放入JPanel。 JPanel将它放入JTextPane。图标和文字的高度相同。