1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| package AllignPerso;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class AllignPerso {
public static void main(String args[]) throws Exception {
JFrame Fenetre = new JFrame("JTextPane Exemples");
Fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
StyleContext context = new StyleContext();
StyledDocument Document = new DefaultStyledDocument(context);
JTextPane textPane = new JTextPane(Document);
textPane.setEditable(true);
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Fenetre.add(scrollPane, BorderLayout.CENTER);
Fenetre.setExtendedState(Frame.MAXIMIZED_BOTH);
Fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Fenetre.setVisible(true);
StyledDocument Doc = textPane.getStyledDocument();
addStylesToDocument(Doc);
// Insertion des chaînes
String chaine1 = new String("\n السَّلاَمُ عَالَيْكُم \n");
String chaine2 = new String("Bonjour\n");
String chaine3 = new String("Hello World\n");
String def6Aff = new String("\n\nL'Amour est une Joie qu'accompagne l'idée d'une cause extérieure.");
String chaineLonguePourVerifScroll = new String ("\n\n\tCette Définition explique assez clairement l'essence de l'Amour ; pour celle des Auteurs qui définissent l'Amour comme la volonté qu'a l'amant de se joindre à la chose aimée, elle n'exprime pas l'essence de l'Amour mais sa propriété, et, n'ayant pas assez bien vu l'essence de l'Amour, ces Auteurs n'ont pu avoir non plus aucun concept clair de sa propriété ; ainsi est-il arrivé que leur définition a été jugée extrêmement obscure par tous. Il faut observer, toutefois, qu'en disant que cette propriété consiste dans la volonté qu'a l'amant de se joindre à la chose aimée, je n'entends point par volonté un consentement, ou une délibération, c'est-à-dire un libre décret (nous avons démontré Proposition 48, Partie II, que c'était là une chose fictive), non pas même un Désir de se joindre à la chose aimée quand elle est absente, ou de persévérer dans sa présence quand elle est là ; l'amour peut se concevoir en effet sans l'un ou sans l'autre de ces Désirs ; mais par volonté j'entends le Contentement qui est dans l'amant à cause de la présence de la chose aimée, contentement par où la Joie de l'amant est fortifiée ou au moins alimentée.\n");
String spinoza = new String("\nSpinoza. Ethique III Def 6 des Affections.\n");
int pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine1, Document.getStyle("regular"));
Style logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine1.length(), Doc.getStyle("regular"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine2, Document.getStyle("bold"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine2.length(), Doc.getStyle("bold"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine3, Document.getStyle("SimpAra36Center"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine3.length(), Doc.getStyle("SimpAra36Center"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine1, Document.getStyle("SimpAra36Right"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine3.length(), Doc.getStyle("SimpAra36Right"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine2, Document.getStyle("SimpAra24boldCenterRed"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine2.length(), Doc.getStyle("SimpAra24boldCenterRed"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine3, Document.getStyle("SimpAra24LeftBlueUnderline"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine3.length(), Doc.getStyle("SimpAra24LeftBlueUnderline"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaine1, Document.getStyle("SimpAra72CenterGreen"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaine1.length(), Doc.getStyle("SimpAra72CenterGreen"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), def6Aff, Document.getStyle("SimpAra24LeftBlueUnderline"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, def6Aff.length(), Doc.getStyle("SimpAra24LeftBlueUnderline"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), chaineLonguePourVerifScroll, Document.getStyle("regular"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, chaineLonguePourVerifScroll.length(), Doc.getStyle("regular"), true);
Doc.setLogicalStyle(pos, logicalStyle);
pos = Doc.getLength();
Document.insertString(Document.getLength(), spinoza, Document.getStyle("SimpAra36Right"));
logicalStyle = Doc.getLogicalStyle(pos);
Doc.setParagraphAttributes(pos, spinoza.length(), Doc.getStyle("SimpAra36Right"), true);
Doc.setLogicalStyle(pos, logicalStyle);
}
public static void addStylesToDocument(StyledDocument doc) {
Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = doc.addStyle("regular", def);
StyleConstants.setFontFamily(def, "Simplified Arabic");
StyleConstants.setFontSize(regular, 18);
StyleConstants.setAlignment(regular, StyleConstants.ALIGN_LEFT);
Style bold = doc.addStyle("bold", regular);
StyleConstants.setFontSize(bold, 22);
StyleConstants.setBold(bold, true);
// On repart toujours de l'intial : regular
Style SimpAra36Center = doc.addStyle("SimpAra36Center", regular);
StyleConstants.setFontSize(SimpAra36Center, 36);
StyleConstants.setAlignment(SimpAra36Center, StyleConstants.ALIGN_CENTER);
Style SimpAra36Right = doc.addStyle("SimpAra36Right", regular);
StyleConstants.setFontSize(SimpAra36Right, 36);
StyleConstants.setAlignment(SimpAra36Right, StyleConstants.ALIGN_RIGHT);
Style SimpAra24boldCenterRed = doc.addStyle("SimpAra24boldCenterRed", regular);
StyleConstants.setFontSize(SimpAra24boldCenterRed, 24);
StyleConstants.setBold(bold, true);
StyleConstants.setAlignment(SimpAra24boldCenterRed, StyleConstants.ALIGN_CENTER);
StyleConstants.setForeground(SimpAra24boldCenterRed, new Color(190, 90, 90));
Style SimpAra24LeftBlueUnderline = doc.addStyle("SimpAra24LeftBlueUnderline", regular);
StyleConstants.setFontSize(SimpAra24LeftBlueUnderline, 24);
StyleConstants.setUnderline(SimpAra24LeftBlueUnderline, true);
StyleConstants.setAlignment(SimpAra24LeftBlueUnderline, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(SimpAra24LeftBlueUnderline, new Color(10, 10, 190));
Style SimpAra72CenterGreen = doc.addStyle("SimpAra72CenterGreen", regular);
StyleConstants.setFontSize(SimpAra72CenterGreen, 72);
StyleConstants.setAlignment(SimpAra72CenterGreen, StyleConstants.ALIGN_CENTER);
StyleConstants.setBackground(SimpAra72CenterGreen, new Color(10, 190, 10));
}
} |
Partager