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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CopyOfTestCardLayout
{
public static void main(String args[])
{
final JFrame frm = new JFrame();
frm.getContentPane().setLayout(new CardLayout());
frm.setSize(600, 400);
// centre jframe
frm.setLocationRelativeTo(null);
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
JPanel pn1 = new JPanel(new FlowLayout());
pn1.setBackground(Color.RED);
//setlayout on place le boutton
pn1.setLayout(null);
JPanel pn2 = new JPanel(new FlowLayout());
pn2.setBackground(Color.GREEN);
// //setlayout on place le boutton
pn2.setLayout(null);
JPanel pn3 = new JPanel(new FlowLayout());
pn3.setBackground(Color.BLUE);
// //setlayout on place le boutton
pn3.setLayout(null);
JLabel lbl1 = new JLabel("Premier panel");
//pour afficher le label en utulisant un setlayout null
lbl1.setBounds(50, 50, 100, 50);
JLabel lbl2 = new JLabel("Deuxième panel");
lbl2.setBounds(50, 50, 100, 50);
JLabel lblQ1 = new JLabel("Premiere Question :...");
lblQ1.setBounds(200, 50, 300, 50);
JLabel lbl3 = new JLabel("Troisième panel:le questionnaire est fini");
lbl3.setBounds(50, 50, 300, 50);
JButton btn1 = new JButton("panel2");
btn1.setBounds(400, 300, 100, 50);
JButton btnquest1 = new JButton("Réponse 1");
btnquest1.setBounds(50, 150, 100, 50);
JButton btnquest2 = new JButton("Réponse 2");
btnquest2.setBounds(175, 150, 100, 50);
JButton btnquest3 = new JButton("Réponse 3");
btnquest3.setBounds(300, 150, 100, 50);
JButton btnquest4 = new JButton("Réponse 4");
btnquest4.setBounds(425, 150, 100, 50);
JButton btn2 = new JButton("panel1");
btn2.setBounds(100, 300, 100, 50);
JButton btn3 = new JButton("panel3");
btn3.setBounds(400, 300, 100, 50);
JButton btn4 = new JButton("Retour panel2");
btn4.setBounds(100, 300, 150, 50);
pn1.add(lbl1);
pn1.add(btn1);
pn2.add(lbl2);
pn2.add(lblQ1);
pn2.add(btn2);
pn2.add(btn3);
pn2.add(btnquest1);
pn2.add(btnquest2);
pn2.add(btnquest3);
pn2.add(btnquest4);
pn3.add(lbl3);
pn3.add(btn4);
frm.getContentPane().add(pn1, "panel1"); //c'est ce texte qui sera utilisé lors de l'appel à la méthode show
frm.getContentPane().add(pn2, "panel2"); //idem
frm.getContentPane().add(pn3, "panel3");
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout)(frm.getContentPane().getLayout());
cl.show(frm.getContentPane(), "panel2");
}
});
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout)(frm.getContentPane().getLayout());
cl.show(frm.getContentPane(), "panel1");
}
});
btn3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout)(frm.getContentPane().getLayout());
cl.show(frm.getContentPane(), "panel3");
}
});
btn4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
CardLayout cl = (CardLayout)(frm.getContentPane().getLayout());
cl.show(frm.getContentPane(), "panel2");
}
});
btnquest1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Réponse 1" );
}
});
btnquest2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Réponse 2" );
}
});
btnquest3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Réponse 3" );
}
});
btnquest4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Réponse 4" );
}
});
}
} |
Partager