Utilisation du jpanel ...
Bonjour, voila j'ai une fenêtre basique :
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
| // beaucoup d'import...
public class Fenetre extends JFrame{
private JPanel pan = new JPanel();
private JButton bouton = new JButton("Nouveau");
private JButton bouton2 = new JButton("Charger");
public Fenetre(){
this.setTitle("Profil");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
pan.add(bouton);
pan.add(bouton2);
this.setContentPane(pan);
this.setVisible(true);
}
} |
et j'aimerai changer sa mise en forme grace a ce 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
|
panel_titre = new PanelTitre("CHOIX DU PROFIL");
FlowLayout(FlowLayout.CENTER,60,100);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
//Decoupage panel qui prendra 100% de la largeur de la FRAME et 10% en hauteur
gbc.weightx = 1.0 ;
gbc.weighty = 0.1;
gbc.fill = GridBagConstraints.BOTH;
//Permet de créer un tour blanc entre les panels et autour des panels
gbc.insets = new Insets (0,0,0,0);
this.add(panel_titre, gbc);
gbc.weightx = 1.0 ;
gbc.weighty = 0.8;
panel_bouton = new JPanel(this);
this.add(panel_bouton,gbc);
gbc.weightx = 1.0 ;
gbc.weighty = 0.1;
panel_quitter = new PanelQuitter(this);
this.add(panel_quitter,gbc); |
Que dois-je faire pour 'fusionner' les deux?
j'aimerai aussi intégrer ce code , créant un tableau a remplir dans une fenêtre :
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
| Variable membre de la classe JPanel
private static JLabel saisi = new JLabel("Saisir réponse");
private static JTextField tf1 = new JTextField("0",5);
private static JTextField tf2 = new JTextField("0",5);
Code permettant de remplir un JPanel avec un JLabel et 2 JTextField
this.setLayout(new GridBagLayout());
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
//Posionnement en position 0,0 dans le tableau imaginaire.
c.gridx = 0;
c.gridy = 0;
//Espacement entre les éléments Haut ,Gauche, Bas, Droite du tableau
c.insets = new Insets (0,50,10,0);
saisi.setForeground(Color.white);
this.add(saisi, c);
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
//Deplacement Haut Gauche Bas Droite
c.insets = new Insets (0,0,10,0);
this.add(tf2, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
c.gridx = 2;
c.gridy = 0;
c.insets = new Insets (0,20,10,50);
this.add(tf2, c); |
et affichant l'heure n'importe où (en bas de la fenêtre par exemple) grâce a ce 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
| import java.util.Date;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Compteur extends Thread {
private JLabel lab;
public Compteur(JLabel lab){
this.lab=lab;
}
public void run(){
while(true){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
Date d=new Date();
lab.setText(d.toString());
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} |
A vrai dire je ne comprends pas comment les jpanel marchent donc j'appelle à votre aide s'il vous plait !