1 pièce(s) jointe(s)
Positionnement des components JPanel
Bonjour,
Je commence à m'intéresser aux interface graphique avec java mais je ne comprend pas comment fonctionne JPanel ou setBounds ou simplement les components.
Je souhaite crée une interface graphique pour un system de tchat déjà fonctionnel mais comme vous pouvez le voir sur l'image et dans le code, j'implémente le JTextField dans un panel qui grâce au setBounds est sensé être redimensionné cependant il apparait en haut sans prendre en compte la redimension du setBounds.
Pouvez-vous m'expliquer comment faire pourque les components soit dans le Panel choisis.
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
|
package fr.dinge;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main {
public static Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
public static int Swidth = (int)size.getWidth();
public static int Sheight = (int)size.getHeight();
public static void main(String[] main) {
Accueil.show();
}
}
class Accueil {
public static int width = 1100;
public static int height = 700;
private static int newX = 0;
private static int newY = 0;
private static int newwidth = 1100;
private static int newheight = 700;
public static void show() {
JFrame frame = new JFrame();
frame.setTitle("SH Project");
frame.setBounds((Main.Swidth/2-width/2), (Main.Sheight/2-height/2), width, height);
frame.setBackground(Color.WHITE);
JPanel Pfriend = Friends();
frame.add(Pfriend);
JPanel header = Header();
frame.add(header);
JPanel contenu = Contenu();
frame.add(contenu);
frame.setUndecorated(true);
frame.setResizable(false);
frame.setVisible(true);
}
private static JPanel Friends() {
int width = 250;
JPanel Panel = new JPanel();
Panel.setBounds(0, 0, width, newheight);
Panel.setBorder(null);
Panel.setBackground(Color.RED);
newX += width;
return Panel;
}
private static JPanel Header() {
int height = 100;
JPanel Panel = new JPanel();
Panel.setBounds(newX, newY, newwidth, height);
Panel.setBorder(null);
Panel.setBackground(Color.GREEN);
newheight -= height;
newY += height;
return Panel;
}
private static JPanel Contenu() {
JPanel Panel = new JPanel();
Panel.setBounds(newX, newY, newwidth, newheight);
Panel.setBackground(Color.BLUE);
Panel.add(new JTextField("Bonjour"));
return Panel;
}
} |
Pièce jointe 612671
il faut utiliser le contentPane
bonsoir,
ayant eu un problème similaire au début, je te propose mon idée.
Il est malvenu de travailler directement dans une fenêtre pour y installer des panels ou autre directement. Le résultat est souvent non conforme, voir aléatoire.
Avant toute chose, il faut récupérer le "contentPane", c'est à dire, la surface utile disponible pour l'utilisateur, que java gère tout seul, et y affecter un conteneur que tu créés.
Ensuite, tu oublies ta fenêtre et tu ne travailles que sur la "zone utile", qui est le JPanel que tu as attribué pour cette zone.
Voila un code possible :
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
|
public class Main {
public static Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
public static int Swidth = (int)size.getWidth();
public static int Sheight = (int)size.getHeight();
public static void main(String[] main) {
Accueil.show();
}
}
class Accueil {
public static int width = 1100;
public static int height = 700;
private static int newX = 0;
private static int newY = 0;
private static int newwidth = 1100;
private static int newheight = 700;
public static void show() {
JFrame frame1 = new JFrame();
frame1.setTitle("SH Project");
frame1.setBounds((Main.Swidth/2-width/2), (Main.Sheight/2-height/2), width, height);
// frame1.setBackground(Color.WHITE);
JPanel frame2 = new JPanel();
frame2.setBackground(Color.white);
frame2.setLayout(null);
frame1.setContentPane(frame2);
JPanel Pfriend = Friends();
frame2.add(Pfriend);
JPanel header = Header();
frame2.add(header);
JPanel contenu = Contenu();
frame1.add(contenu);
// frame1.setUndecorated(true);
frame1.setResizable(false);
frame1.setVisible(true);
}
private static JPanel Friends() {
int width = 250;
JPanel Panel = new JPanel();
Panel.setBounds(0, 0, width, newheight);
Panel.setBorder(null);
Panel.setBackground(Color.RED);
newX += width;
return Panel;
}
private static JPanel Header() {
int height = 100;
JPanel Panel = new JPanel();
Panel.setBounds(newX, newY, newwidth, height);
Panel.setBorder(null);
Panel.setBackground(Color.GREEN);
newheight -= height;
newY += height;
return Panel;
}
private static JPanel Contenu() {
JPanel Panel = new JPanel();
Panel.setBounds(newX, newY, newwidth, newheight);
Panel.setBackground(Color.BLUE);
JTextField texte = new JTextField("Bonjour" + " newX = " + newX + " et newY = " + newY);
Panel.add(texte);
return Panel;
}
} |
De plus, pour le rendu que tu souhaites , tu peux utliser le layout "BorderLayout" pour ton panel et dispatcher facilement tes panels :
- à droite pour le rouge : tu l'ajoutes au contentPane dans la zone de gauche (west)
- en haut pour le panneau vert ( North)
- partout sur l'espace restant (Center)
J'espère que cela te sera utile.
Bonne soirée.