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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FenetreBoutonsActions extends JFrame implements ActionListener
{
//Création de tous les boutons nécessaires dans le programme
private JButton bouton0;
private JButton bouton1;
private JButton bouton2;
private JButton bouton3;
private JButton bouton4;
private JButton bouton5;
private JButton bouton6;
private JButton bouton7;
private JButton bouton8;
private JButton bouton9;
private JButton boutonP;
private JButton boutonM;
private JButton boutonX;
//COnstructeur de ma classe où j'appel "build" qui va créer ma fenêtre principale
public FenetreBoutonsActions()
{
super();
build();
}
//Création de la fenêtre
private void build()
{
setTitle("Fenêtre");//Titre de la fenêtre
setSize(500,240);//Taille de la fenêtre
setLocationRelativeTo(null);//Positionnement de la fenêtre au milieu de l'écran
setResizable(false);//Empêche l'user de modifier la taille de la fenêtre
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Permet de terminer le processus lorsque l'user presse la croix rouge
setContentPane(ContentPane());//Appel le contentPane qui va faire lire les actions aux boutons créés ci-dessus
}
private JPanel ContentPane()
{
//On créer un panel
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
boutonX = new JButton("X");
boutonX.addActionListener(this);
panel2.add(boutonX);
boutonM = new JButton("-");
boutonM.addActionListener(this);
panel2.add(boutonM);
boutonP = new JButton(".");
boutonP.addActionListener(this);
panel2.add(boutonP);
//JButton bouton0 = new JButton(new ZAction("0"));
bouton0 = new JButton("0");
bouton0.addActionListener(this);
panel2.add(bouton0);
//JButton bouton = new JButton(new IciAction("1")); Méthode pour afficher l'image dans une autre fenêtre (avec une autre classe)
//Il faut enregistrer l'action lorsque le bouton est pressé pour pouvoir ensuite afficher l'image correspondante (ici "1")
bouton1 = new JButton("1");
bouton1.addActionListener(this);
panel2.add(bouton1);
//JButton bouton2 = new JButton(new LaAction("2"));
bouton2 = new JButton("2");
bouton2.addActionListener(this);
panel2.add(bouton2);
//JButton bouton3 = new JButton(new AAction("3"));
bouton3 = new JButton("3");//La parenthèse après JButton permet d'afficher ce texte sur le bouton
bouton3.addActionListener(this);
panel2.add(bouton3);
//JButton bouton4 = new JButton(new BAction("4"));
bouton4 = new JButton("4");
bouton4.addActionListener(this);
panel2.add(bouton4);
//JButton bouton5 = new JButton(new CAction("5"));
bouton5 = new JButton("5");
bouton5.addActionListener(this);
panel2.add(bouton5);
//JButton bouton6 = new JButton(new DAction("6"));
bouton6 = new JButton("6");
bouton6.addActionListener(this);
panel2.add(bouton6);
//JButton bouton7 = new JButton(new EAction("7"));
bouton7 = new JButton("7");
bouton7.addActionListener(this);
panel2.add(bouton7);
//JButton bouton8 = new JButton(new FAction("8"));
bouton8 = new JButton("8");
bouton8.addActionListener(this);
panel2.add(bouton8);
//JButton bouton9 = new JButton(new GAction("9"));
bouton9 = new JButton("9");
bouton9.addActionListener(this);
panel2.add(bouton9);
JLabel label = new JLabel("Bienvenue");
panel2.add(label);
return panel2;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
int lkj = 0 ;
JLabel img1 = new JLabel();
if(source.equals(boutonX))
{
System.out.println("i=1");
lkj = 1;
System.out.println(lkj);
}
if(lkj == 1)
{
System.out.println("OUI!");
remove(img1);
}
else if(source.equals(bouton9))
{
ImageIcon icon1 = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb9.png");
img1.setIcon(icon1);
this.add(img1);
this.setVisible(true);
}
else if(source.equals(bouton1))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb1.png");
JLabel img2 = new JLabel();
img2.setIcon(icon);
this.add(img2);
this.setVisible(true);
}
else if(source.equals(bouton0))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb0.png");
JLabel img3 = new JLabel();
img3.setIcon(icon);
this.add(img3);
this.setVisible(true);
}
else if(source.equals(bouton2))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb2.png");
JLabel img4 = new JLabel();
img4.setIcon(icon);
this.add(img4);
this.setVisible(true);
}
else if(source.equals(bouton3))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb3.png");
JLabel img5 = new JLabel();
img5.setIcon(icon);
this.add(img5);
this.setVisible(true);
}
else if(source.equals(bouton4))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb4.png");
JLabel img6 = new JLabel();
img6.setIcon(icon);
this.add(img6);
this.setVisible(true);
}
else if(source.equals(bouton5))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb5.png");
JLabel img7 = new JLabel();
img7.setIcon(icon);
this.add(img7);
this.setVisible(true);
}
else if(source.equals(bouton6))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb6.png");
JLabel img8 = new JLabel();
img8.setIcon(icon);
this.add(img8);
this.setVisible(true);
}
else if(source.equals(bouton7))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb7.png");
JLabel img9 = new JLabel();
img9.setIcon(icon);
this.add(img9);
this.setVisible(true);
}
else if(source.equals(bouton8))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb8.png");
JLabel img10 = new JLabel();
img10.setIcon(icon);
this.add(img10);
this.setVisible(true);
}
else if(source.equals(boutonP))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nbP.png");
JLabel img11 = new JLabel();
img11.setIcon(icon);
this.add(img11);
this.setVisible(true);
}
else if(source.equals(boutonM))
{
ImageIcon icon = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nbM.png");
JLabel img12 = new JLabel();
img12.setIcon(icon);
this.add(img12);
this.setVisible(true);
}
}
} |