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

 Java Discussion :

Mon bouton ne s'affiche pas correctement


Sujet :

Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2013
    Messages : 5
    Par défaut Mon bouton ne s'affiche pas correctement
    Bonjours à tous !

    Voilà mon problème, j'ai un petit problème avec de mes boutons je m'explique en gros dans mon JPanel j'ai créer un nouveau bouton

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    private static SpllitButton but = new SpllitButton(0);
    Voici la class de mon bouton
    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
    package net.minecraft;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
     
    public class SpllitButton extends JButton
     
     
      implements ActionListener, MouseListener
    {
      private static final long serialVersionUID = 1L;
      int type = 0;
      Image img;
     
      public SpllitButton(int type)
      {
        this.type = type;
        addActionListener(this);
        addMouseListener(this);
     
        if (type == 0)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("Login.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    /*	else if (type == 1)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("optionsbutton.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	else if (type == 2) {
          try {
    		this.img = ImageIO.read(LauncherFrame.class.getResource("minimizebutton.png"));
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
        }*/
        setOpaque(true);
        ;
        setBounds(450, 300, 99, 28);
        setBorder(BorderFactory.createEmptyBorder());
      }
     
      public void setEnabled(boolean b)
      {
        super.setEnabled(b);
      }
     
      public void paintComponent(Graphics g)
      {
        g.drawImage(this.img, 0, 0, getWidth(), getHeight(), this);
      }
     
     
      public void mouseClicked(MouseEvent event)
      {
      }
     
      public void mouseEntered(MouseEvent event)
      {
        if (this.type == 0)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("LoginC.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    /*	else if (this.type == 1)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("optionsbuttonhover.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	else if (this.type == 2)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("minimizebuttonhover.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}*/
      }
     
      public void mouseExited(MouseEvent event)
      {
        if (this.type == 0)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("Login.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	/*else if (this.type == 1)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("optionsbutton.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	else if (this.type == 2)
    		try {
    			this.img = ImageIO.read(LauncherFrame.class.getResource("minimizebutton.png"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}*/
      }
     
      public void mousePressed(MouseEvent event)
      {
      }
     
      public void mouseReleased(MouseEvent event)
      {
      }
     
    @Override
    public void actionPerformed(ActionEvent e) {
    	// TODO Auto-generated method stub
     
    }
    }
    Et je l'ai intégrer dans mon JPanel dans ma méthode initMenu() comme ceci :
    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
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    package net.minecraft;
     
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Checkbox;
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Desktop;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.HeadlessException;
    import java.awt.Image;
    import java.awt.Insets;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.Rectangle;
    import java.awt.TextField;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    import java.awt.image.FilteredImageSource;
    import java.awt.image.VolatileImage;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Random;
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.PBEParameterSpec;
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JEditorPane;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
     
    public class LoginForm extends JPanel
    {
    private int x = 225;
    private int y = 10;
      private static final long serialVersionUID = 1L;
      private Image bgImage;
      private JTextField userName;
      private JPasswordField password;
     
      private static SpllitButton but = new SpllitButton(0);
     
      private JCheckBox rememberBox = new JCheckBox("Retenir mot de passe");
     
      private JButton launchButton = new JButton("Login");
     
      private JButton offlineButton = new JButton("Jouer hors ligne");
     
      private JEditorPane news = new JEditorPane();
     
      private JButton options = new JButton("Options");
     
      private Label errorLabel = new Label("", 1);
      private LauncherFrame launcherFrame;
      private boolean outdated = false;
     
      private Image fondtroll = null;
     
      private Image fondoutdate = null;
     
      BufferedImage image = null;
      private Music sound_1 = new Music("lol");
     
      public LoginForm()
      {
     
    	setBackground(Color.black);
     
     
      setLayout(null);
        initMenu();
       // playSong(sound_1);
        //readUsername();
     
        this.but.addActionListener(new ActionListener() 
        {
        	public void actionPerformed(ActionEvent ae) 
    		{
    			if (userName.getText().isEmpty() || password.getText().isEmpty())
    			{
    				javax.swing.JOptionPane.showMessageDialog(null,"Veuillez compléter les champs requis"); 
     
    			}
    			else
        		launcherFrame.login(userName.getText(), password.getText());
     
    		}
        }
        );
    }
     
      public void initMenu()
      {
    	  try
    	    {
    	      this.bgImage = ImageIO.read(LoginForm.class.getResource("BackgroundLauncher.png"));
    	    } catch (IOException e) {
    	      e.printStackTrace();
    	    }
     
    	    this.userName = new JTextField(39);
    	    this.userName.setBounds(275, 180, 250, 15);
     
    	    this.userName.setFocusable(true);
    	    this.userName.setForeground(Color.BLACK);
    	    this.userName.setCaretColor(Color.BLACK);
    	    this.userName.setBorder(BorderFactory.createEmptyBorder());
    	    this.userName.setOpaque(true);
    	    add(this.userName);
     
    	    this.password = new JPasswordField(39);
    	    this.password.setBounds(275, 214+30, 275, 15);  
    	    this.password.setForeground(Color.BLACK);
    	    this.password.setBorder(BorderFactory.createEmptyBorder());
    	    this.password.setFocusable(true);
    	    this.password.setOpaque(true);
    	    add(this.password);
     
     
    	    add(this.but);
     
     
     
     
      }
     
     
      public void playSong(Music sound)
      {
      	sound.start();
      }
     
    private void readUsername() 
    {
      try {
        File lastLogin = new File(Util.getWorkingDirectory(), "lastlogin");
        Cipher cipher = getCipher(2, "passwordfile");
        DataInputStream dis;
      if (cipher != null)
          dis = new DataInputStream(new CipherInputStream(new FileInputStream(lastLogin), cipher));
      else {
          dis = new DataInputStream(new FileInputStream(lastLogin));
        }
        this.userName.setText(dis.readUTF());
        this.password.setText(dis.readUTF());
     
        dis.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
     
    private void writeUsername() 
    {
      try {
        File lastLogin = new File(Util.getWorkingDirectory(), "lastlogin");
        Cipher cipher = getCipher(1, "passwordfile");
        DataOutputStream dos;
      if (cipher != null)
        dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(lastLogin), cipher));
      else {
        dos = new DataOutputStream(new FileOutputStream(lastLogin));
      }
        dos.writeUTF(this.userName.getText());
        dos.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
     
      private Cipher getCipher(int mode, String password) throws Exception {
        Random random = new Random(43287234L);
        byte[] salt = new byte[8];
        random.nextBytes(salt);
        PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);
     
        SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(password.toCharArray()));
        Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
        cipher.init(mode, pbeKey, pbeParamSpec);
        return cipher;
      }
     
      public void update(Graphics g) {
        paint(g);
     
      }
     Image font;
      public void paint(Graphics g2)
      {
     
    	 super.paint(g2);
     
        ImageIcon gif;
        int w = getWidth() ;
        int h = getHeight() ;
     
     
        try {
    		font = ImageIO.read(LoginForm.class.getResource("font.jpg"));
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
     
     
     
      g2.drawImage(font,0,0,w,h, null);
      g2.drawImage(this.bgImage,20+x,50+y, null);
     
        g2.dispose();
     
     
      }
     
     
     
      public void setError(String errorMessage) {
        removeAll();
     
        this.errorLabel.setText(errorMessage);
        validate();
      }
     
      public void loginOk() {
        writeUsername();
      }
     
      public void setNoNetwork() {
        removeAll();
     
        validate();
      }
     
      public void checkAutologin() {
        if (this.password.getText().length() > 0)
          this.launcherFrame.login(this.userName.getText(), this.password.getText());
      }
     
      public void setOutdated()
      {
        this.outdated = true;
      }
    }
    Mais le problème c'est que lorsque je lance mon application mon bouton ne s'affiche pas tant que je ne passe pas ma souris au moins 1 fois sur sa position ! Dés lors que j'ai passer ma souris il fonctionne très bien ...

    Avez-vous une idée pour résoudre ce problème ?

  2. #2
    Membre chevronné
    Homme Profil pro
    Inscrit en
    Janvier 2010
    Messages
    312
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 312
    Par défaut
    Bonjour,

    Peut-etre un g.dispose() dans la méthode paintComponent de ton SplitButton??

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2013
    Messages : 5
    Par défaut
    Nop sa ne change rien :/

  4. #4
    Membre chevronné
    Homme Profil pro
    Inscrit en
    Janvier 2010
    Messages
    312
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 312
    Par défaut
    Je pense qu'au premier affichage, l'image n'a pas le temps de charger avant l'affichage.

    Après, comme elle est chargée il n'y a plus de problème.

    tu peux tester en mettant un delay dans le constructeur du spllitButton par exemple pour laisser le temps a l'image de charger.

    Si c'est le cas, 2 possibilités pour suivre le chargement de ton image

    Implementer la methode imageUpdate dans ta classe SpllitButton.

    Tu peux le faire car dans la méthode drawImage, tu définis le dernier paramètre par this ce qui signifie que ta classe est un observer du chargement.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    public boolean imageUpdate(Image img,int infoFlags,
        int x,int y,int width,int height) {
        if((infoFlags & ImageObserver.ALLBITS)==ALLBITS) {
          repaint();
          return false;
        }
        else return true;
      }
    Autre méthode MediaTracker.

    Je te laisse regarder si cela te tente

  5. #5
    Modérateur

    Homme Profil pro
    Développeur java, access, sql server
    Inscrit en
    Octobre 2005
    Messages
    2 713
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Développeur java, access, sql server
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2005
    Messages : 2 713
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    private static SpllitButton but = new SpllitButton(0);
    pourquoi static ???
    Labor improbus omnia vincit un travail acharné vient à bout de tout - Ambroise Paré (1510-1590)

    Consulter sans modération la FAQ ainsi que les bons ouvrages : http://jmdoudoux.developpez.com/cours/developpons/java/

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Mai 2013
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2013
    Messages : 5
    Par défaut
    Merci enfaîte j'ai fait une erreur au niveau de mon Panel ! Merci quand même

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

Discussions similaires

  1. Réponses: 9
    Dernier message: 31/05/2011, 10h27
  2. [EasyPHP] Mon texte ne s'affiche pas correctement.
    Par solaar dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 6
    Dernier message: 26/03/2011, 11h31
  3. mon bouton ne s'affiche pas
    Par naim2009 dans le forum Interfaces Graphiques
    Réponses: 1
    Dernier message: 29/12/2008, 14h13
  4. La 1ère ligne dans mon tableau ne s'affiche pas correctement
    Par bilou95 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 19/03/2007, 16h33
  5. [JApplet]mon applet ne s'affiche pas.
    Par yannick56 dans le forum Applets
    Réponses: 6
    Dernier message: 27/05/2004, 17h05

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