2012-10-04 27 views
3

我需要在我的GUI中同步我的PDF ListItem和JTextArea的行間距。我可以通過調整這個或另一個來做到這一點。 (或JTextArea)不止一行(在JTextArea中將行換行設置爲true)。iText列表項目或JTextArea中的行間距

我可以調整兩個ListItems之間的高度。該距離也將應用於單行多列ListItem的兩行之間的高度。

但是,在我的GUI中,由於JTextArea中的組件鏜孔和默認行間距,這兩個不一樣。差異大約是一個像素,但大規模可能會積累並導致一些問題。

那麼,有沒有什麼辦法可以在JTextArea中設置行距,或者我可以區分兩個列表項和同一列表項的兩行之間的空格?

我一切,爲使用任何種類和任何一種可能需要技巧的外部庫...

+0

你可以發表一些代碼嗎? – linski

回答

4

要重寫JTextArea的行間距,請查看PlainView(用於呈現PLainDocument)。

裏有public void paint(Graphics g, Shape a)方法

 drawLine(line, g, x, y); 
     y += fontHeight; 

以下行這樣你就可以適應渲染固定y偏移。

BasicTextAreaUI方法中創建視圖。用自己實現的PlainView

public View create(Element elem) { 
    Document doc = elem.getDocument(); 
    Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/); 
    if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { 
     // build a view that support bidi 
     return createI18N(elem); 
    } else { 
     JTextComponent c = getComponent(); 
     if (c instanceof JTextArea) { 
     JTextArea area = (JTextArea) c; 
     View v; 
     if (area.getLineWrap()) { 
      v = new WrappedPlainView(elem, area.getWrapStyleWord()); 
     } else { 
      v = new PlainView(elem); 
     } 
     return v; 
     } 
    } 
    return null; 
} 
+0

非常直觀+1 – mKorbel

+0

對不起,我很抱歉,但paint(Graphics g,Shape a)方法和create(Element elem)方法之間的連接在哪裏? 爲什麼我必須實現PlainView的verson,我不能只改變paint(args)方法嗎? – Karlovsky120

+0

恐怕不是。渲染實際上由View完成。 paint()調用視圖的paint() – StanislavL

0

你可以嘗試使用JTextPane代替JTextArea並設置行間距相應看像你的ListItems。

SimpleAttributeSet set = new SimpleAttributeSet(); 

StyleConstants.setLineSpacing(set, 0.5f); // <--- your value here 

textPane.setParagraphAttributes(set, true); 

這不會影響現有文本的行間距,所以您應該隨後設置文本。

1

更換的控制權使用ParagraphAttribute SpaceBelow你行之間的空間。您可以使用4行代碼或更少的代碼(請參閱下面的示例)。您將需要使用JTextPane來使用這些ParagraphAttributes(但JTextPane和JTextArea`非常相似,您不應該注意到區別)。

import java.awt.*; 
import javax.swing.*; 
import javax.swing.text.*; 


public class LineSpacingExample extends Box{ 
    JTextPane modifiedTA; 
    //Modify this to whatever spacing you want between the rows. 
    static final float spaceBelow = 5.0f; 
    //Font height - automatically calculated by code below 
    int fontHeight = 0; 

    public LineSpacingExample(){ 
     super(BoxLayout.X_AXIS); 

     //Demonstrating that the spacing is predictable 
     final JPanel leftBox = new CustomBox(); 
     add(leftBox); 

     //Sets the amount of space below a row (only code you need to add) 
     DefaultStyledDocument pd = new DefaultStyledDocument(); 
     SimpleAttributeSet sas = new SimpleAttributeSet(); 
     StyleConstants.setSpaceBelow(sas, spaceBelow); 
     pd.setParagraphAttributes(0, pd.getLength(), sas, false); 

     modifiedTA= new JTextPane(pd); 
     add(modifiedTA); 

     //Calculates the font height in pixels 
     fontHeight = modifiedTA.getFontMetrics(modifiedTA.getFont()).getHeight(); 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     final JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new LineSpacingExample()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    //EXTRA! Paints the left hand side box - to show that the spacing is predictable in pixels 
    public class CustomBox extends JPanel{ 

     public CustomBox() { 
      super(); 
      setOpaque(true); 
      setBackground(Color.orange); 

     } 

     public void paintComponent(Graphics g){ 
      super.paintComponent(g); 
      int height = getSize().height; 
      int drawLocation = 2; //To account for padding on the TextPane 
      int row = 1; 
      while(drawLocation < height){ 

       //Drawing the text row background 
       g.setColor(Color.blue); 
       g.fillRect(0, drawLocation, 50, fontHeight); 

       //Drawing the text row number 
       g.setColor(Color.white); 
       g.drawString(Integer.toString(row++), 0, drawLocation+14); 
       drawLocation += fontHeight; 

       //Drawing the space row 
       g.setColor(Color.green); 
       g.fillRect(0, drawLocation, 50, (int)spaceBelow); 
       drawLocation += spaceBelow; 
      } 
     } 
    }; 

}