2017-09-11 7 views
-1

merci pour l'aide que vous pouvez me prêter, j'ai un peu de temps à chercher la solution pour l'espacement entre les lignes de paragraphed dans un jtextpane et j'ai appliqué plusieurs suggestions sans succès pour modifier l'espacement entre les lignes, alors j'indique le code que j'ai fait pour vos opinions. Mercijava jtextpane set gap ou setleading ou setlinespacing

import java.awt.Font; 
import java.awt.FontMetrics; 
import java.awt.Insets; 
import javax.swing.JFrame; 
import javax.swing.JTextPane; 
import javax.swing.text.Element; 
import javax.swing.text.MutableAttributeSet; 
import javax.swing.text.SimpleAttributeSet; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyledDocument; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
/** 
* 
* @author Usuario 
*/ 
public class jtextpane7 extends JTextPane { 

    public jtextpane7() { 
     super(); 
     super.setFont(new Font("Arial", Font.ITALIC, 14)); 
     super.setMargin(new Insets(10, 5, 10, 5)); //top, left, bottom, right 

    } 
    // @Override 
    // public FontMetrics getFontMetrics(Font font) { 
    ////  FontMetrics f = super.getFontMetrics(font); 
    ////  return new FontMetricsWrapper(f) { 
    ////   @Override 
    ////   public int getHeight() { 
    ////    return 10; // Gives line height in pixels 
    ////   } 
    ////  }; 

    @Override 
    public void setText(String t) { 
     super.setText(setStyle(t)); //To change body of generated methods, choose Tools | Templates. 
    } 

    private String setStyle(String t) { 
     return "<div align='justify' width='50px' height='120px' style='font-family:Arial; font-size:14px; line-height:150%;'>" + t + "</div>"; 
    } 

    public static void main(String[] args) { 

     jtextpane7 editor = new jtextpane7(); 
     editor.setBounds(0, 0, 500, 500); 
     editor.setContentType("text/html"); 
     editor.setText(" <html>\n" 
       + " <head>\n" 
       + " \n" 
       + " </head>\n" 
       + " <body>\n" 
       + " <div align=\"justify\" width=\"50px\" height=\"120px\" style=\"line-height: 15px\">\n" 
       + "  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, e del a&#241;o 2017 prueba de linea ancho\n" 
       + " </div>\n" 
       + " </body>\n" 
       + "</html> "); 
    editor.selectAll(); 
    MutableAttributeSet set = new SimpleAttributeSet(editor.getParagraphAttributes()); 
    StyleConstants.setLineSpacing(set, 55.0f);//55.0f value test failed 
    editor.setCaretPosition(0); 
    editor.setParagraphAttributes(set, false); 


     JFrame frame = new JFrame(); 
     frame.setBounds(30, 30, 600, 600); 
     frame.setVisible(true); 
     //frame.pack(); 
     frame.setLayout(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(editor); 

    } 
} 

class FontMetricsWrapper extends FontMetrics { 

    private final FontMetrics target; 

    public FontMetricsWrapper(FontMetrics target) { 
     super(target.getFont()); 
     this.target = target; 
    } 

    @Override 
    public int bytesWidth(byte[] data, int off, int len) { 
     return target.bytesWidth(data, off, len); 
    } 

    @Override 
    public int charWidth(char ch) { 
     return target.charWidth(ch); 
    } 

    @Override 
    public int charWidth(int codePoint) { 
     return target.charWidth(codePoint); 
    } 

    @Override 
    public int getHeight() { 
     return super.getLeading() + super.getAscent() + super.getDescent() + 10; 
    } 
} 
+0

Avez-vous essayé editor.setParagraphAttributes (set, true); ? – Assafs

+1

oui, j'ai essayé et rien –

Répondre

2

N'utilisez pas HTML. Au lieu de cela, utilisez du texte normal et ajoutez des attributs avec le texte.

Exemple simple:

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

public class TextPaneAttributes extends JPanel 
{ 

    public TextPaneAttributes() 
    { 
     setLayout(new BorderLayout()); 

     JTextPane textPane = new JTextPane(); 
     textPane.setText("one\ntwo\nthree\nfour\nfive\nsix\nseven\neight"); 

//  DefaultHighlighter highlighter = (DefaultHighlighter)textPane.getHighlighter(); 
//  highlighter.setDrawsLayeredHighlights(false); 

     // Define some character and paragraph attributes 

     SimpleAttributeSet keyWord = new SimpleAttributeSet(); 
     StyleConstants.setBold(keyWord, true); 

     SimpleAttributeSet green = new SimpleAttributeSet(); 
     StyleConstants.setForeground(green, Color.GREEN); 

     SimpleAttributeSet center = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER); 

     SimpleAttributeSet left = new SimpleAttributeSet(); 
     StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT); 

     // Change attributes on some existing text 

     StyledDocument doc = textPane.getStyledDocument(); 
     doc.setCharacterAttributes(0, 3, keyWord, false); 
     doc.setCharacterAttributes(8, 5, green, true); 
     doc.setParagraphAttributes(20, 1 , center, false); 

     // Add some text with attributes 

     try 
     { 
      doc.insertString(doc.getLength(), "\nNormal text", null); 
      doc.insertString(doc.getLength(), "\nGreen text centered", green); 
      doc.setParagraphAttributes(doc.getLength(), 1 , center, false); 
      doc.insertString(doc.getLength(), "\nKeyword text", keyWord); 
      doc.setParagraphAttributes(doc.getLength(), 1 , left, false); 

      // Newly typed text at the end of the document will inherit the 
      // "keyword" attributes unless we remove the attributes 

      textPane.setCaretPosition(doc.getLength()); 
      textPane.getInputAttributes().removeAttributes(keyWord); 
     } 
     catch(Exception e) {} 

     // Add text pane to frame 

     JScrollPane scrollPane = new JScrollPane(textPane); 
     scrollPane.setPreferredSize(new Dimension(200, 250)); 
     add(scrollPane); 

     // Create a Button panel 

     JPanel buttons = new JPanel(); 
     add(buttons, BorderLayout.PAGE_END); 

     // Add a Bold button 

     JButton bold = new JButton(new StyledEditorKit.BoldAction()); 
     buttons.add(bold); 

     // Add Right Alignment button 

     JButton right = new JButton(new StyledEditorKit.AlignmentAction("Align Right", StyleConstants.ALIGN_RIGHT)); 
     buttons.add(right); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new TextPaneAttributes()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(() -> createAndShowGUI()); 
/* 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
*/ 
    } 
} 

Il y a aussi des attributs que vous pouvez utiliser pour définir l'espacement des lignes.

+0

merci j'ai essayé et commenter la solution. –

+0

le code de la solution, et merci mon ami "camickr" –

+0

@NelsonEduardo, content que cela a aidé. N'oubliez pas "d'accepter" ma réponse en cliquant sur la coche afin que les gens sachent que le problème a été résolu. – camickr