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

AWT/Swing Java Discussion :

boutton de dessin(b, u, i, justification de texte)


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2005
    Messages
    201
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2005
    Messages : 201
    Par défaut boutton de dessin(b, u, i, justification de texte)
    Salut tout le monde.
    J'ai un gros problemme, j'arrive pas a faire le code de certain boutton les voici :
    Boutton gras B,
    // italique I,
    // souligné U,
    Justification du texte : centré, droite, gauche.
    J'ai essayé d'introduire le code HTML mais je sais pas comment le faire.
    ps : je travail avec un JTextPane.

  2. #2
    Membre émérite
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Par défaut
    essaye ca, c'est pas difficile.
    regarde la partie en rouge!

    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
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.StyledEditorKit;
    
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     * Date: 30-Sep-2006
     * Time: 20:38:33
     * To change this template use File | Settings | File Templates.
     */
    public class JTextPaneTestFrame extends JFrame {
    
        public JTextPaneTestFrame() {
    
            JTextPane myTextPane = new JTextPane();
            StyledDocument doc = myTextPane.getStyledDocument();
    
            try {
                doc.insertString(0, "The attacks were planned by the ISI and carried out by the Islamist militant group Lashkar-e-Toiba, based in Pakistan, Mumbai's police chief said.\n" +
                        "AN Roy said the Students' Islamic Movement of India had also assisted.\n" +
                        "Pakistan rejected the allegations and said India had given no evidence of Pakistani involvement in the attacks.\n" +
                        "\"We have solved the 11 July bombings case. The whole attack was planned by Pakistan's ISI and carried out by Lashkar-e-Toiba and their operatives in India,\" Mumbai (Bombay) police commissioner AN Roy told a news conference", null);
            } catch (BadLocationException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
    
            JScrollPane myScrollPane = new JScrollPane();
            myScrollPane.setPreferredSize(new Dimension(400, 300));
    
            myScrollPane.setViewportView(myTextPane);
            add(myScrollPane);
    
            JButton button = new JButton("Bold");
            add(button, BorderLayout.SOUTH);
    
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    new StyledEditorKit.BoldAction().actionPerformed(null);
    //                new StyledEditorKit.ItalicAction.actionPerformed(null);
    //                new StyledEditorKit.UnderlineAction.actionPerformed(null);
    //                new StyledEditorKit.AlignmentAction("gauche", StyleConstants.ALIGN_LEFT);
                }
            });
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JTextPaneTestFrame myFrame = new JTextPaneTestFrame();
                    myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    myFrame.pack();
                    myFrame.setLocationRelativeTo(null);
                    myFrame.setVisible(true);
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
        }
    }
    (texte copy-paster depuis bbc.co.uk)

  3. #3
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2005
    Messages
    201
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2005
    Messages : 201
    Par défaut
    MERCI c'est ce qu'il me fallait; mais comme meme j'ai une question :
    a force de faire des new dans l'actionListener, tu crois pas que l'application va prendre beaucoup de place dans la memoire.?
    Qu'est ce que t'en dit?
    Si je fait le GarbedgCollector dans l'actionListener, est ce que cela va me raisoudre le problemme?

    Merci encor

  4. #4
    Membre émérite
    Avatar de bbclone
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2006
    Messages : 537
    Par défaut
    les code que je met c'est des truc fait en 3-4 minutes.
    c'est pas souvent les meilleur facon de faire; c'est juste pour montrer la voie !!;-)

    mais t'inquiete pas de la gestion de la memoire quand tu develope. c'est le travail du GC :-)

    si je devais faire ce que tu as demander, je passerai par des Action.
    parce que souvent mettre en gras ca a aussi une touche de raccourci. (CTRL + B par exemple)
    voila le meme code qu'hier un peu changer.

    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
    import javax.swing.Action;
    import javax.swing.InputMap;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.KeyStroke;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.StyledDocument;
    import javax.swing.text.StyledEditorKit;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    /**
     * Created by IntelliJ IDEA.
     * User: bebe
     * Date: 30-Sep-2006
     * Time: 20:38:33
     * To change this template use File | Settings | File Templates.
     */
    public class JTextPaneTestFrame extends JFrame {
    
        public JTextPaneTestFrame() {
    
            JTextPane myTextPane = new JTextPane();
            StyledDocument doc = myTextPane.getStyledDocument();
    
            try {
                doc.insertString(0, "The attacks were planned by the ISI and carried out by the Islamist militant group Lashkar-e-Toiba, based in Pakistan, Mumbai's police chief said.\n" +
                        "AN Roy said the Students' Islamic Movement of India had also assisted.\n" +
                        "Pakistan rejected the allegations and said India had given no evidence of Pakistani involvement in the attacks.\n" +
                        "\"We have solved the 11 July bombings case. The whole attack was planned by Pakistan's ISI and carried out by Lashkar-e-Toiba and their operatives in India,\" Mumbai (Bombay) police commissioner AN Roy told a news conference", null);
            } catch (BadLocationException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
    
            JScrollPane myScrollPane = new JScrollPane();
            myScrollPane.setPreferredSize(new Dimension(400, 300));
    
            myScrollPane.setViewportView(myTextPane);
            add(myScrollPane);
    
            JPanel myButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    
            /**************************************************************************************************************/
            JButton button = new JButton("<html><b>B</b></html>");
            button.setRequestFocusEnabled(false);
            
            Action action = new StyledEditorKit.BoldAction();
            action.putValue(Action.NAME, "Bold");
            action.putValue(Action.SHORT_DESCRIPTION, "An action to ...");   // -> this will be the tooltip on a button ;-)
    //        action.putValue(..., ...); icon, accelerator...
    
            InputMap inputMap = myTextPane.getInputMap();
            inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.CTRL_MASK), action);
    
            button.setAction(action);
            
            myButtonPanel.add(button);
            /**************************************************************************************************************/
    
    
            /**************************************************************************************************************/
            button = new JButton("<html><i>I</i></html>");
            button.setRequestFocusEnabled(false);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    new StyledEditorKit.ItalicAction().actionPerformed(null);
                }
            });
            myButtonPanel.add(button);
            /**************************************************************************************************************/
    
            add(myButtonPanel, BorderLayout.PAGE_END);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    JTextPaneTestFrame myFrame = new JTextPaneTestFrame();
                    myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    myFrame.pack();
                    myFrame.setLocationRelativeTo(null);
                    myFrame.setVisible(true);
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
        }
    }
    maintenant dans ton textpane si tu selectione un texte et tu fais CTRL + B, c'est comme si tu clique sur le bouton B.

  5. #5
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2005
    Messages
    201
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2005
    Messages : 201
    Par défaut
    OK J'ai rien a jouter.
    Tout ce que je peut dire c'est merci

  6. #6
    Candidat au Club
    Inscrit en
    Février 2007
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 3
    Par défaut Justifier à droite le texte dans un JtextPane
    Bonjour,

    j'ai lu avec attention votre discussion forte intéressante, mais j'ai un petit problème.
    En fait j'affiche du texte dans mon jTextPane de la même manière que ce que vous le faites et bien entendu ce texte n'est pas justifié.

    Auriez-vous une solution pour justifier à droite ce texte?

    merci

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

Discussions similaires

  1. justifer un texte
    Par druzy dans le forum Composants
    Réponses: 1
    Dernier message: 13/09/2008, 08h34
  2. problème de justification de texte
    Par marion5515 dans le forum Mise en page CSS
    Réponses: 1
    Dernier message: 16/05/2008, 14h36
  3. Justification de texte avec citation
    Par Ryu1200 dans le forum Mise en forme
    Réponses: 2
    Dernier message: 11/12/2007, 17h32
  4. Justification de texte (Ne pas couper les mots)
    Par akrogames dans le forum C++
    Réponses: 1
    Dernier message: 28/01/2007, 21h42
  5. Dessiner un rectangle avec bords et texte ...
    Par Djedjeridoo dans le forum Composants VCL
    Réponses: 3
    Dernier message: 16/06/2003, 17h17

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