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 :

Insertion des composants dans une JPanel


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Février 2009
    Messages
    15
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 15
    Par défaut Insertion des composants dans une JPanel
    Bonsoir
    je suis débutant en java
    je voulais créer une gestion adhérent et dans la première forme j'ai inséré une image dans un JPanel et je voulais ajouter les autres composants tels que JLabel et JtextField .. sur cette JPanel
    mais tout vas bien !! SAUF quand je réduis et je restaure la forme je vois que les composants se doublent

    voici 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
    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
    public class authentification extends JFrame{
     
     
    	paneau pan;
    	 JTextField login;
    	JLabel ok ;
    	JPasswordField password;
     
    public static void main(String[] args) {
     
    	authentification at=new authentification();
     
    	at.setVisible(true);
    }
     
     
    public authentification(){
    	try {
     
     
    		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
    		setLocation(((d.width)/2)-300,((d.height)/2)-152);
    		setSize(600,304);
    		setLayout(null);
    		setTitle("login");
    		setContentPane(new paneau());
    	} catch (Exception e) {
    		// TODO: handle exception
    	}
     
    }
     
    class paneau extends JPanel{
    	public paneau(){
    		super();
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		try {
    			BufferedImage image = ImageIO.read(new File("login.jpg"));
    			g.drawImage(image, 0, 0, null);
     
    			Font f=new Font("Arial",Font.BOLD+Font.ITALIC,15);
    			ImageIcon ico=new ImageIcon("ok.png");
    			login=new JTextField(15);
    			login.setBorder(BorderFactory.createLineBorder(Color.black));
     
    			login.setFont(f);
    			login.setForeground(Color.black);
    			login.setBackground(Color.DARK_GRAY);
    			login.setBounds(175, 87,136,20);
    			password=new JPasswordField(15);
    			password.setBorder(BorderFactory.createLineBorder(Color.black));
    			password.setBounds(175, 127,136,20);
    			password.setForeground(Color.black);
    			password.setBackground(Color.DARK_GRAY);
    			password.setFont(f);
    			ok=new JLabel(ico);
    			ok.setBounds(320, 165,24,23);
    			ok.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) );
    			add(login);
    			add(ok);
    			add(password);
     
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
     
     
     
    	}
    }
    }
    -------------------------------------------------------------------------



    MERCI d'avance

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Par défaut
    C'est normal, tu ajoutes tes composants dans la méthode "paintComponent" et cette méthode est appelée à chaque fois que son composant a besoin d'être rafraichi. L'ajout des composants doit se faire dans le constructeur de "paneau".
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
    Que la force de la puissance soit avec le courage de ta sagesse.

  3. #3
    Membre Expert
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 050
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 050
    Par défaut
    Salut,

    De plus, tu gagnerais en performance si tu instancie ton image depuis le constructeur (1 seul fois) plutôt qu'a chaque fois dans le paint (qui peut être dessiné plusieurs fois).
    Voici le code modifié, cela t'aidera peut-être à y voir plus clair :
    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
    import java.awt.Color;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
     
    public class Authentification extends JFrame {
     
    	Paneau pan;
    	JTextField login;
    	JLabel ok;
    	JPasswordField password;
     
    	public static void main(String[] args) {
    		Authentification at = new Authentification();
    		at.setVisible(true);
    	}
     
    	public Authentification() {
    		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    		setLocation(((d.width) / 2) - 300, ((d.height) / 2) - 152);
    		setSize(600, 304);
    		setLayout(null);
    		setTitle("login");
    		setContentPane(new Paneau());
    	}
     
    	class Paneau extends JPanel {
     
    		BufferedImage image;
     
    		public Paneau() {
    			super();
    			try {
    				image = ImageIO.read(new File("login.jpg"));
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			Font f = new Font("Arial", Font.BOLD + Font.ITALIC, 15);
    			ImageIcon ico = new ImageIcon("ok.png");
    			login = new JTextField(15);
    			login.setBorder(BorderFactory.createLineBorder(Color.black));
     
    			login.setFont(f);
    			login.setForeground(Color.black);
    			login.setBackground(Color.DARK_GRAY);
    			login.setBounds(175, 87, 136, 20);
    			password = new JPasswordField(15);
    			password.setBorder(BorderFactory.createLineBorder(Color.black));
    			password.setBounds(175, 127, 136, 20);
    			password.setForeground(Color.black);
    			password.setBackground(Color.DARK_GRAY);
    			password.setFont(f);
    			ok = new JLabel(ico);
    			ok.setBounds(320, 165, 24, 23);
    			ok.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    			add(login);
    			add(ok);
    			add(password);
    		}
     
    		public void paintComponent(Graphics g) {
    			super.paintComponent(g);
    			g.drawImage(image, 0, 0, null);
    		}
    	}
    }

  4. #4
    Membre averti
    Inscrit en
    Février 2009
    Messages
    15
    Détails du profil
    Informations forums :
    Inscription : Février 2009
    Messages : 15
    Par défaut Insertion des composants dans une JPanel : résolu :)
    merci énormément pour votre réponse car elle m'a aidé à trouver la bonne réponse puisque j'ai ajouté les composant dans le constructeur de la classe elle même en ajoutant le "setLayout(null)" , Merci beaucoup vraiment j'ai beaucoup entendu sur ce forum et c'est vraiment chapeau

    voila mon nouveau 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
    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
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Cursor;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    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.io.File;
    import java.io.IOException;
    import java.sql.*;
     
    import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
     
    public class authentification extends JFrame implements ActionListener{
     
    	static Connection con=null;
    	paneau pan;
    	JTextField login;
    	Image font;
    	JLabel ok ;
    	JPasswordField password;
    public static void main(String[] args) {
    	getConnection();
    	authentification at=new authentification();
    	at.setVisible(true);
    }
     
    public static void getConnection(){
    	try {
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    		String str="jdbc:odbc:club";
    		try {
    			con=DriverManager.getConnection(str,"","");
    			System.out.println("Connection effectué avec succés !");
    		} catch (SQLException e) {
    			System.out.println("Echec de connection !!");
    			e.printStackTrace();
    		}
    	} catch (ClassNotFoundException e) {
    		// TODO Bloc catch auto-généré
    		e.printStackTrace();
    	}
     
    }
    public authentification(){
    	setSize(600,304);
    	Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
    	setLocation((d.width/2)-300,(d.height/2)-152);
    	setUndecorated(true);
    	//insertion de l'image
    	font=Toolkit.getDefaultToolkit().getImage("login.jpg");
    	MediaTracker mt=new MediaTracker(this);
    	mt.addImage(font, 0);
    	try {
    		mt.waitForAll();
    	} catch (InterruptedException e) {
    		// TODO Bloc catch auto-généré
    		e.printStackTrace();
    	}
     
    	setContentPane(new paneau(font));
    	//insertion des composants
        setLayout(null);
    	Container cont=getContentPane();
    	Font f=new Font("Arial",Font.BOLD+Font.ITALIC,15);
    	ImageIcon ico=new ImageIcon("ok.png");
    	login=new JTextField(15);
    	login.setBorder(BorderFactory.createLineBorder(Color.black));
     
    	login.setFont(f);
    	login.setForeground(Color.black);
    	login.setBackground(Color.DARK_GRAY);
    	login.setBounds(175, 87,143,20);
    	password=new JPasswordField(15);
    	password.setBorder(BorderFactory.createLineBorder(Color.black));
    	password.setBounds(175, 127,143,20);
    	password.setForeground(Color.black);
    	password.setBackground(Color.DARK_GRAY);
    	password.setFont(f);
    	ok=new JLabel(ico);
    	ok.setBounds(320, 165,24,23);
    	ok.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) );
    	ok.addMouseListener(new MouseAdapter(){
    		public void mouseClicked(MouseEvent arg) {
    			String s="select * from utilisateur where login='"+login.getText()+"' and password='"+password.getText()+"'";
    			try {
    				Statement st=con.createStatement();
    				ResultSet rs=st.executeQuery(s);
    				while (rs.next()){
    					home h=new home();
    					h.show();
     
    				}
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
     
    		}
    	});
    	cont.add(login);
    	cont.add(ok);
    	cont.add(password);
     
    }
    class paneau extends JPanel{
    	Image image;
    	public paneau(Image fn){
    		super();
    		image=fn;
     
    	}
    	public void paintComponent(Graphics g){
    		try {
    			g.drawImage(image, 0, 0, null);
     
    		} catch (Exception e) {
     
    			e.printStackTrace();
    		}
     
     
    	}
    }
    public void actionPerformed(ActionEvent ev) {
     
    	String s="select * from utilisateur where login='"+login.getText()+"' and password='"+password.getText()+"'";
    if(ev.getSource()==ok){
     
     
    }
     
    }
     
    }


    ----------------------------------------------------------

    bon début de semaine

Discussions similaires

  1. probléme d'insertion des variables dans une table
    Par moooona dans le forum Installation
    Réponses: 2
    Dernier message: 30/03/2008, 14h08
  2. Centrer des composants dans une form?
    Par alg_dev dans le forum Delphi
    Réponses: 1
    Dernier message: 10/06/2007, 10h24
  3. insertion des valeurs dans une colonne
    Par freestyler1982 dans le forum Langage SQL
    Réponses: 1
    Dernier message: 24/01/2007, 16h38
  4. Placer correctement des Composants dans un JPanel
    Par Lady dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 14/01/2007, 13h34
  5. [VB6]problème d'insertion des donneés dans une base d'Access2003
    Par lanbok dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 27/05/2006, 12h17

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