[Débutant] construction d'un cadre
Bonjour
Dans ma tete ce code sert à afficher une fenetre contenant deux JPanel. Chaque JPanel reçoit deux JButton.
Ca fait 3 jours que je tourne en rond.
Lorsque je lance la méthode main, j'obitens une fenetre grise. Quelqu'un peut-il me dire ce que j'ai mal fait ?
J'utilise BlueJ.
D'avance merci.
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
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingPanel3
{
// déclaration des variables d'intsance
SwingPanelCompos spc1 , spc2;
JPanel pane = new JPanel();
// construit un objet SwingPanel3.
public SwingPanel3()
{
// mise en page utilisée pour pane BoxLayout les composants sur une même ligne
pane.setLayout(new BoxLayout(pane,BoxLayout.X_AXIS));
// texte des boutons du panneau 1
String texteb1a = new String ("Bouton 1a");
String texteb1b = new String ("Bouton 1b");
// instance de SwingPanelCompos ajoutée au conteneur pane
spc1 = new SwingPanelCompos(texteb1a,texteb1b);
pane.add(spc1);
// texte des boutons du panneau 2
String texteb2a = new String ("Bouton 2a");
String texteb2b = new String ("Bouton 2b");
// instance de SwingPanelCompos ajoutée au conteneur pane
spc2 = new SwingPanelCompos(texteb2a,texteb2b);
pane.add(spc2);
}
public static void main (String[] args)
{
JFrame fenetre = new JFrame("SwingPanel3");
SwingPanel3 sp3 = new SwingPanel3();
fenetre.setContentPane(sp3.pane);
fenetre.setVisible(true);
}
}
class SwingPanelCompos extends JPanel
{
// variables d'instance
JPanel p = new JPanel();
JButton ba;
JButton bb;
// construit un Objet SwingPanelCompos, et récupère en arguments le texte des boutons
SwingPanelCompos(String txt1,String txt2)
{
ba = new JButton(txt1);
bb = new JButton(txt2);
// mise en page BoxLayout en colonne
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
// boutons ba et bb ajoutés au panneau p
p.add(ba);
p.add(bb);
}
} |