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 :

[Debutant] Un affichage un peu aléatoire.. ?


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    71
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 71
    Par défaut [Debutant] Un affichage un peu aléatoire.. ?
    Bonjour !

    Ce dessous la source de ma classe FenetreLancement.java :

    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
     
     
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
     
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class FenetreLancement extends JPanel{
     
    	/** Attributs */
     
    	private JFrame fenetre;
    	private JButton tempsSession;
    	private JButton tempsAffichage;
    	private JButton tempsEteint;
    	private JLabel labelTempsSession;
    	private JLabel labelTempsAffichage;
    	private JLabel labelTempsEteint;
    	private JCheckBox afficherScore;
    	private JButton demarrerSession;
    	private JButton afficherStats;
    	private JButton quitter;
    	private JButton reinitialiser;
    	private JTextField tempsSessionField;
     
    	public FenetreLancement() {
        // Create and specify a layout manager
    	build();
    	fenetre.setLayout(new GridBagLayout());
     
        // Create a constraints object, and specify some default values
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH; // components grow in both dimensions
        c.insets = new Insets(5, 5, 5, 5); // 5-pixel margins on all sides
     
        // Create and add a bunch of buttons, specifying different grid
        // position, and size for each.
        // Give the first button a resize weight of 1.0 and all others
        // a weight of 0.0. The first button will get all extra space.
        tempsSession = new JButton("Temps Session");
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(tempsSession, c);
     
        tempsAffichage= new JButton("Temps Affichage");
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(tempsAffichage, c);
     
        tempsEteint = new JButton("Temps Eteint");
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(tempsEteint, c);
     
        labelTempsSession = new JLabel("Durée 0");
        c.gridx = 3;
        c.gridy = 0;
        c.gridwidth = 4;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(labelTempsSession, c);
     
        labelTempsAffichage = new JLabel("Durée 1");
        c.gridx = 3;
        c.gridy = 1;
        c.gridwidth = 4;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(labelTempsAffichage, c);
     
        labelTempsAffichage = new JLabel("Durée 2");
        c.gridx = 3;
        c.gridy = 2;
        c.gridwidth = 4;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(labelTempsAffichage, c);
     
        reinitialiser = new JButton("Réinitialiser");
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(reinitialiser, c);
     
        afficherScore = new JCheckBox("Afficher les points");
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 4;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(afficherScore, c);
     
        demarrerSession = new JButton("Commencer");
        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(demarrerSession, c);
     
        afficherStats = new JButton("Statistiques");
        c.gridx = 2;
        c.gridy = 5;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(afficherStats, c);
     
        quitter = new JButton("Quitter");
        c.gridx = 4;
        c.gridy = 5;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(quitter, c);
     
        tempsSessionField = new JTextField("300");
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.weightx = 0; 
        c.weighty = 0;
        fenetre.add(tempsSessionField, c);
     
      }
     
    	private void build(){
     
    		fenetre = new JFrame();
    		fenetre.setTitle("Entrainement");
    		fenetre.setSize(640,480);
    		fenetre.setLocationRelativeTo(null);
    		fenetre.setResizable(true);
    		fenetre.setVisible(true);
     
    	}
     
      public static void main(String[] a) {
     
    	FenetreLancement f = new FenetreLancement();
     
      }
    }
    Lors de l'execution de celle-ci, rare sont les fois où tous les elements sont affichés d'un coup (il manque des boutons...), et quand il en manque (9 fois sur 10) il suffit de resizer la fenetre avec la souris et ceux-ci apparaissent.

    A quoi cela est-il du ?
    Comment y remedier ?

    Merci !

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 904
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 904
    Billets dans le blog
    54
    Par défaut
    1) Les elements ne doivent pas etre ajoute au contenu de la fenetre APRES son affichage auquel cas il faut revalider le contenu via un appel a revalidate() (ce qui est fait par le redimensionnement de la fenetre). Mieux vaut construire la fenetre AVANT l'affichage.

    2) Swing utilise sa propre Thread, l'Event Dispatch Thread, et il n'est pas recommande d'initialiser et manipuler des composants Swing dans la Thread du main(). Il faut donc utiliser le squelette suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
      public static void main(String[] a) {
        // Start at EDT.
        SwingUtilities.invokeLater(new Runnable() {
          /** {@inheritDoc}
           */
          public void run() {  
            FenetreLancement f = new FenetreLancement();
          }
        });
      }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    71
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 71
    Par défaut Merci !
    Merci !
    Ça fonctionne à la perfection !
    Salut !

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 24/11/2006, 17h08
  2. [Debutant] Probleme affichage d'une image
    Par iutcien dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 16/11/2006, 15h13
  3. Réponses: 10
    Dernier message: 07/06/2006, 11h12
  4. [DEBUTANT][HTML] affichage d'une image dans une page
    Par nimport nawak dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 12/04/2005, 19h50
  5. [Info][Debutant(e)]affichage temps reel
    Par nine dans le forum Développement Web en Java
    Réponses: 15
    Dernier message: 26/11/2004, 17h03

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