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 :cry:
voici le code :
----------------------------------------------------------------
Code:
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 ;)
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 :king:
voila mon nouveau code :
--------------------------------------------------------------------
Code:
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