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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
| import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
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;
JPanel panel2 = new JPanel();
JLabel img1 = new JLabel();
JLabel img23 = new JLabel();
JLabel img2 = new JLabel();
JLabel img3 = new JLabel();
JLabel img4 = new JLabel();
JLabel img5 = new JLabel();
JLabel img6 = new JLabel();
JLabel img7 = new JLabel();
JLabel img8 = new JLabel();
JLabel img9 = new JLabel();
JLabel img10 = new JLabel();
JLabel img11 = new JLabel();
JLabel img12 = new JLabel();
JLabel imgf = new JLabel();
int z, i, j;
//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
this.setContentPane(panel2());//Appel le contentPane qui va faire lire les actions aux boutons créés ci-dessus
}
/*public void paintComponent(Graphics g)
{
Toolkit kit = Toolkit.getDefaultToolkit();
Image imgH = kit.getImage("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/fond.png");
g.drawImage(imgH, 100, 15, 300, 50, null);
}*/
private JPanel panel2()
{
//On créer un panel
panel2.setLayout(null);
//panel2.setLayout(new FlowLayout());
ImageIcon iconF = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/fond.png");
imgf.setIcon(iconF);
imgf.setSize(300, 50);
imgf.setBounds(100, 15, 300, 50);
this.add(imgf);
this.setVisible(true);
boutonX = new JButton("X");
boutonX.setBounds(100, 60, 40, 30);
boutonX.addActionListener(this);
panel2.add(boutonX);
boutonM = new JButton("-");
boutonM.setSize(20, 30);
boutonM.setBounds(170, 60, 40, 30);
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 ;
if(source.equals(boutonX))
{
System.out.println("i=1");
lkj = 1;
System.out.println(lkj);
remove(img1);
}
if(lkj == 1)
{
System.out.println("OUI!");
remove(img1);
ImageIcon icon2 = new ImageIcon("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/sup.png");
img23.setIcon(icon2);
this.add(img23);
this.setVisible(true);
}
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
img12.setIcon(icon);
this.add(img12);
img12.setSize(40, 30);
img12.setBounds(130, 15, 40, 30);
this.setVisible(true);
}
}
/*public void paintComponent(Graphics g)
{
try
{
System.out.println("OUIIII");
//int x1 = this.getWidth()/4;
// int y1 = this.getHeight()/4;
Image imgf = ImageIO.read(new File("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/fond.png"));
g.drawImage(imgf, 100, 8, this);
if (z == 0)
{
Image img0 = ImageIO.read(new File("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb0.png"));
g.drawImage(img0, 125, 15, this);
}
Image img1 = ImageIO.read(new File("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb1.png"));
g.drawImage(img1, 195, 15, this);
Image img2 = ImageIO.read(new File("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb2.png"));
g.drawImage(img2, 270, 15, this);
Image img3 = ImageIO.read(new File("C:/Users/admstage.FORMATION/workspace/Fenetre/bin/nb3.png"));
g.drawImage(img3, 340, 15, this);
}catch (IOException e){
e.printStackTrace();
}
}*/
} |