IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

JTextArea alignement à droite [Débutant(e)]


Sujet :

Composants Java

  1. #1
    Membre chevronné Avatar de dmganges
    Homme Profil pro
    Retraité. Ne recherche pas un emploi.
    Inscrit en
    Septembre 2011
    Messages
    1 392
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Retraité. Ne recherche pas un emploi.
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2011
    Messages : 1 392
    Points : 2 044
    Points
    2 044
    Par défaut JTextArea alignement à droite
    Bonjour,
    Dans un JTextArea j'aurai du français et de l'arabe mélangé.
    J'utilise setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    1- Il y a une anomalie : le "." de la première ligne se trouve à gauche.
    2- Ça ne correspond pas à ce que je veux faire, car je souhaite modifier la justification sur chaque "append", et je n'ai pas trouvé de méthode sur le JTextArea qui fasse un justify...
    3- à la fin des "append" le curseur se trouve positionné sur la première ligne, je voudrais l'avoir, prêt à insérer : derrière le dernier caractère de la dernière ligne.

    Voici un extrait de code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package Test;
    import javax.swing.*;
    import java.awt.*;
    public class Test {
    	public static void main(String[] args) {
     
    		// create the line wrap example PAS AFFICHE !
    		// JTextArea first = new JTextArea(all);
    		// first.setLineWrap(true);
     
    		JFrame f = new JFrame("Test");
    		Font font = new Font("Simplified Arabic", Font.BOLD, 36);
    		JTextArea second = new JTextArea();
    		second.setForeground(Color.BLUE);
    		second.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    		second.setFont(font);
    		second.setLineWrap(true);
    		JScrollPane trois = new JScrollPane(second);
    		trois.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		//second.append("\n");
    		second.append("La saison été.");
    		second.append("\n");
     
    		second.append("\t");
    		second.append("فَصْلُ  الصَّيْف");	
    		second.append("\n");
    		second.append("TITI\n");
    		// f.add(first);
    		f.add(trois);
    		// f.setSize(400, 300);
    		f.setExtendedState(Frame.MAXIMIZED_BOTH);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    	}
    }
    et une image de ce que j'obtiens.
    En général le texte français devrait être centré, et le texte arabe cadré à droite.
    MERCI d'avance pour vos suggestions !

  2. #2
    Membre chevronné Avatar de dmganges
    Homme Profil pro
    Retraité. Ne recherche pas un emploi.
    Inscrit en
    Septembre 2011
    Messages
    1 392
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Retraité. Ne recherche pas un emploi.
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2011
    Messages : 1 392
    Points : 2 044
    Points
    2 044
    Par défaut
    Bonjour,
    Ça va beaucoup mieux avec un JTextPane !

    Je suis parti de http://docs.oracle.com/javase/tutori...ditorpane.html
    Visiblement l'auteur s'attend à ce que le Pig et le Button soient au centre...
    en réalité il sont cadrés à gauche.

    J'ai le même problème :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    package Allignement;
    
    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.Frame;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.text.DefaultStyledDocument;
    import javax.swing.text.Style;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyleContext;
    import javax.swing.text.StyledDocument;
    
    public class Allignement {
    	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);
    
    		Style attributes = context.getStyle(StyleContext.DEFAULT_STYLE);
    		StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_CENTER);
    		StyleConstants.setUnderline(attributes, false);
    		StyleConstants.setFontFamily(attributes, "Simplified Arabic");
    		
    		
    		StyleConstants.setFontSize(attributes, 12);
    		document.insertString(document.getLength(), "\nCOUCOU.", attributes);
    		
    		StyleConstants.setFontSize(attributes, 36);
    		document.insertString(document.getLength(), "\n فَصْلُ  الصَّيْف	", attributes);
    		
    		// ça ne f() ps
    		// StyleConstants.setAlignment(attributes, StyleConstants.ALIGN_LEFT);
    		
    		
    		StyleConstants.setFontFamily(attributes, "SansSerif");
    		StyleConstants.setFontSize(attributes, 16);
    		StyleConstants.setBold(attributes, true);
    		StyleConstants.setItalic(attributes, true);
    		StyleConstants.setUnderline(attributes, true);
    		
    		StyleConstants.setForeground(attributes, Color.RED);
    		StyleConstants.setBackground(attributes, Color.YELLOW);
    			
    		document.insertString(document.getLength(), "\nLa saison été", attributes);
    
    		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);
    	}
    }
    Je colle ici la ligne 33, car à l'intérieur des balises les caractères arabes sont encodés...
    document.insertString(document.getLength(), "\n فَصْلُ الصَّيْف ", attributes);

    Au deuxième StyleConstants.ALIGN_ la totalité du texte se trouve cadré à gauche, je m'attendais à avoir les lignes déjà inscrites toujours au centre...

    Est-il possible dans un JTextPane, d'avoir une justification différente à chaque insertion de string, sans passer par la gestion de position de répertoire...

    MERCI d'avance pour vos suggestions.

  3. #3
    Membre chevronné Avatar de dmganges
    Homme Profil pro
    Retraité. Ne recherche pas un emploi.
    Inscrit en
    Septembre 2011
    Messages
    1 392
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Retraité. Ne recherche pas un emploi.
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2011
    Messages : 1 392
    Points : 2 044
    Points
    2 044
    Par défaut Allignements et Attributs dans un JTextPane
    Bonjour,
    Ce n'est pas un modèle du genre, mais il permet de comprendre...
    Je le colle à toutes fins utiles :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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));
     
    	}
    }
    Après ce n'est que de l'art...
    MD

  4. #4
    Membre chevronné Avatar de dmganges
    Homme Profil pro
    Retraité. Ne recherche pas un emploi.
    Inscrit en
    Septembre 2011
    Messages
    1 392
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Retraité. Ne recherche pas un emploi.
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2011
    Messages : 1 392
    Points : 2 044
    Points
    2 044
    Par défaut JTextePane Oracle Modifié
    Pour celles et ceux qui suivent, un petit ajout.
    Dans http://docs.oracle.com/javase/tutori...ditorpane.html l'image et le button ne sont pas cadrés comme le laisse croire le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    s = doc.addStyle("icon", regular);
    		StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    		ImageIcon pigIcon = createImageIcon("images/Pig.gif", "a cute pig");
    		if (pigIcon != null) {
    			StyleConstants.setIcon(s, pigIcon);
    		}
     
    		s = doc.addStyle("button", regular);
    		StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
    		ImageIcon soundIcon = createImageIcon("images/sound.gif", "sound icon");
    		JButton button = new JButton();
    		if (soundIcon != null) {
    			button.setIcon(soundIcon);
    		} else {
    			button.setText("BEEP");
    		}
    		button.setCursor(Cursor.getDefaultCursor());
    		button.setMargin(new Insets(0, 0, 0, 0));
    		button.setActionCommand(buttonString);
    		button.addActionListener(this);
    		StyleConstants.setComponent(s, button);
    	}
    Pour gérer correctement les alignements il faut passer par une gestion des attributs par paragraphe.
    Ce qui donne, uniquement pour la partie JTextPane :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    package Test_2;
    import javax.swing.*;
    import javax.swing.text.*;
     
    import java.awt.*; //for layout managers and more
    import java.awt.event.*; //for action events
     
    public class Test_2 extends JPanel implements ActionListener {
    	private String newline = "\n";
    	protected static final String buttonString = "JButton";
    	private static final long serialVersionUID = 1;
     
    	public Test_2() {
    		setLayout(new BorderLayout());
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		Dimension Ecran = tk.getScreenSize();
    		int larg = Ecran.width;
    		int haut = Ecran.height;
    		setSize (larg, haut);
     
    		// Lay out the text controls and the labels.
    		GridBagLayout gridbag = new GridBagLayout();
    		JPanel textControlsPane = new JPanel();
    		textControlsPane.setLayout(gridbag);
     
    		// Create a text pane.
    		JTextPane textPane = createTextPane();		
    		JScrollPane paneScrollPane = new JScrollPane(textPane);
    		paneScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    		paneScrollPane.setPreferredSize(new Dimension(larg, haut)); // s'étend sur largeur
     
    		// Put the editor pane and the text pane in a split pane.
    		JPanel rightPane = new JPanel(new GridLayout(1, 0));
    		rightPane.add(paneScrollPane);
     
    		// Put everything together.
    		add(rightPane, BorderLayout.LINE_END);
    	}
     
    	private JTextPane createTextPane() {
    		String[] initString = {
    				"This is an editable JTextPane,	 فَصْلُ  الصَّيْف	" + newline, // regular
    				"another" + newline, // italic
    				"styled" + newline, // bold
    				"text فَصْلُ  الصَّيْف	" + newline, // small
    				"فَصْلُ  الصَّيْف	" + newline, // arabic
    				"component, ", // large
    				"which supports embedded components..." + newline,// regular
    				" " + newline, // button
    				"...and embedded icons..." + newline, // right
    				" " + newline, // icon
    				newline + "JTextPane is a subclass of JEditorPane that "
    						+ "uses a StyledEditorKit and StyledDocument, and provides "
    						+ "cover methods for interacting with those objects.", //regular
    				"\n\nL'Amour est une Joie qu'accompagne l'idée d'une cause extérieure.", // blue
    				"\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", // regular
    		};
     
    		String[] initStyles = {"regular", "italic", "bold", "small", "arabic", "large",
    				"regular", "bouton", "right", "icon", "regular", "blue", "regular"};
     
    		StyleContext context = new StyleContext();
    		StyledDocument Document = new DefaultStyledDocument(context);
    		JTextPane textPane = new JTextPane(Document);
    		StyledDocument doc = textPane.getStyledDocument();
    		addStylesToDocument(doc);
     
    		int pos = 0;
    		try {
    			for (int i = 0; i < initString.length; i++) {
    				doc.insertString(doc.getLength(), initString[i], doc.getStyle(initStyles[i]));
    				doc.setParagraphAttributes(pos, initString[i].length(), doc.getStyle(initStyles[i]), true);
    				pos = doc.getLength();
    			}
    		} catch (BadLocationException ble) {
    			System.err.println("Couldn't insert initial text into text pane.");
    		}
     
    		return textPane;
    	}
     
    	protected void addStylesToDocument(StyledDocument doc) {
     
    		// Initialize some styles.
    		Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
     
    		Style regular = doc.addStyle("regular", def);
    		StyleConstants.setFontFamily(regular, "Simplified Arabic");
    		StyleConstants.setAlignment(regular, StyleConstants.ALIGN_LEFT);
    		StyleConstants.setFontSize(regular, 18);
     
    		Style right = doc.addStyle("right", def);
    		StyleConstants.setFontFamily(right, "Simplified Arabic");
    		StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
    		StyleConstants.setFontSize(right, 18);
     
    		Style italic = doc.addStyle("italic", regular);
    		StyleConstants.setAlignment(regular, StyleConstants.ALIGN_LEFT);
    		StyleConstants.setFontFamily(italic, "Serif");
    		StyleConstants.setFontSize(italic, 28);
    		StyleConstants.setItalic(italic, true);
    		StyleConstants.setBold(italic, true);
     
    		Style bold = doc.addStyle("bold", regular);
    		StyleConstants.setAlignment(bold, StyleConstants.ALIGN_CENTER);
    		StyleConstants.setFontFamily(bold, "SansSerif");
    		StyleConstants.setFontSize(bold, 30);
    		StyleConstants.setBold(bold, true);
     
    		Style small = doc.addStyle("small", regular);
    		StyleConstants.setAlignment(small, StyleConstants.ALIGN_RIGHT);
    		StyleConstants.setFontSize(small, 14);
     
    		Style large = doc.addStyle("large", regular);
    		StyleConstants.setAlignment(large, StyleConstants.ALIGN_LEFT);
    		StyleConstants.setFontSize(large, 16);
     
    		Style icon = doc.addStyle("icon", regular);
    		StyleConstants.setAlignment(icon, StyleConstants.ALIGN_CENTER);
    		ImageIcon pigIcon = createImageIcon("images/Pig.gif", "a cute pig");
    		if (pigIcon != null) {
    			StyleConstants.setIcon(icon, pigIcon);
    		}
     
    		Style bouton = doc.addStyle("bouton", regular);
    		StyleConstants.setAlignment(bouton, StyleConstants.ALIGN_CENTER);
    		ImageIcon soundIcon = createImageIcon("images/sound.gif", "sound icon");
    		JButton button = new JButton();
    		if (soundIcon != null) {
    			button.setIcon(soundIcon);
    		} else {
    			button.setText("BEEP");
    		}
    		button.setCursor(Cursor.getDefaultCursor());
    		button.setMargin(new Insets(0, 0, 0, 0));
    		button.setActionCommand(buttonString);
    		button.addActionListener(this);
    		StyleConstants.setComponent(bouton, button);
     
    		Style arabic = doc.addStyle("arabic", regular);
    		StyleConstants.setFontFamily(arabic, "Simplified Arabic");
    		StyleConstants.setFontSize(arabic, 76);
    		StyleConstants.setAlignment(arabic, StyleConstants.ALIGN_CENTER);
    		StyleConstants.setForeground(arabic, new Color(10, 190, 10));
     
    		Style blue = doc.addStyle("blue", regular);
    		StyleConstants.setFontSize(blue, 24);
    		StyleConstants.setUnderline(blue, true);
    		StyleConstants.setAlignment(blue, StyleConstants.ALIGN_LEFT);
    		StyleConstants.setForeground(blue, new Color(10, 10, 190));
     
    	}
     
    	/** Returns an ImageIcon, or null if the path was invalid. */
    	protected static ImageIcon createImageIcon(String path, String description) {
    		java.net.URL imgURL = Test_2.class.getResource(path);
    		if (imgURL != null) {
    			return new ImageIcon(imgURL, description);
    		} else {
    			System.err.println("Couldn't find file: " + path);
    			return null;
    		}
    	}
     
    	/**
             * Create the GUI and show it. For thread safety, this method should be
             * invoked from the event dispatch thread.
             */
    	private static void createAndShowGUI() {
    		// Create and set up the window.
    		JFrame frame = new JFrame("TextSamplerDemo");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		// Add content to the window.
    		frame.add(new Test_2());
    		frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    		// Display the window.
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		// Schedule a job for the event dispatching thread:
    		// creating and showing this application's GUI.
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				// Turn off metal's use of bold fonts
    				UIManager.put("swing.boldMetal", Boolean.FALSE);
    				createAndShowGUI();
    			}
    		});
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (buttonString.equals(e.getActionCommand())) {
    			Toolkit.getDefaultToolkit().beep();
    		}		
    	}
    }
    MD

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. tableau aligné à droite et CSS
    Par MicroPuce dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 07/09/2010, 16h10
  2. Alignement à droite
    Par David_Prog dans le forum Cobol
    Réponses: 14
    Dernier message: 27/07/2010, 16h21
  3. Div align à droite sort de l'écran
    Par dadzz77 dans le forum Mise en page CSS
    Réponses: 5
    Dernier message: 08/10/2007, 17h17
  4. Tableau, colonne alignée à droite
    Par progfou dans le forum Tableaux - Graphiques - Images - Flottants
    Réponses: 7
    Dernier message: 21/05/2006, 17h09
  5. listbox alignement à droite
    Par Tymk dans le forum C++Builder
    Réponses: 4
    Dernier message: 26/11/2005, 19h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo