slt à toute la communauté,
je suis en train de realiser la calculatrice windows (calc.exe) en java. Voici mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
 
import net.sourceforge.c4j.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
 
@ContractReference (contractClassName = "RechnerContract")
@SuppressWarnings("serial")
public class RechnerGUI extends JFrame
{
    private Container contentPane;
    private final Color  RECHNER_GRAY = new Color(212,208,200); // Farbe Grau
    private final Insets RECHNER_MARGIN_VERTICAL = new Insets(4,2,4,2);  //
    private final Insets RECHNER_MARGIN_HOR = new Insets(4,4,4,4);
    private final Font   RECHNER_FONT = new Font("Arial",0,12);
    private JMenuBar menuBar;
    private JMenu menuBearbeiten;
    private JMenu menuAnsicht;
    private JMenu menuHilfe;
    private JMenuItem menuItemKopieren;
    private JMenuItem menuItemEinfuegen;
    private JRadioButtonMenuItem menuItemStandard;
    private JRadioButtonMenuItem menuItemWissenschaftlich;
    private JMenuItem menuItemZifferngruppierung;
    private JMenuItem menuItemHilfethemen;
    private JMenuItem menuItemInfo;
    private ButtonGroup bgAnsicht;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JPanel panel6;
    private JTextField tfDisplay;
    private JTextField tfM;
    private JButton buttonMC;
    private JButton buttonMR;
    private JButton buttonMS;
    private JButton buttonMPlus;
    private JButton buttonRuecktaste;
    private JButton buttonCE;
    private JButton buttonC;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton buttonDiv;
    private JButton buttonSqrt;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton buttonMul;
    private JButton buttonProzent;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton buttonMinus;
    private JButton buttonKehrwert;
    private JButton button0;
    private JButton buttonPlusMinus;
    private JButton buttonKomma;
    private JButton buttonPlus;
    private JButton buttonGleich;
    private ActionListenerMC alMC;
    private ActionListenerMR alMR;
    private ActionListenerMS alMS;
    private ActionListenerMPlus alMPlus;
    private ActionListenerRuecktaste alRuecktaste;
    private ActionListenerCE alCE;
    private ActionListenerC alC;
    private ActionListener7 al7;
    private ActionListener8 al8;
    private ActionListener9 al9;
    private ActionListenerDiv alDiv;
    private ActionListenerSqrt alSqrt;
    private ActionListener4 al4;
    private ActionListener5 al5;
    private ActionListener6 al6;
    private ActionListenerMul alMul;
    private ActionListenerProzent alProzent;
    private ActionListener1 al1;
    private ActionListener2 al2;
    private ActionListener3 al3;
    private ActionListenerMinus alMinus;
    private ActionListenerKehrwert alKehrwert;
    private ActionListener0 al0;
    private ActionListenerPlusMinus alPlusMinus;
    private ActionListenerKomma alKomma;
    private ActionListenerPlus alPlus;
    private ActionListenerGleich alGleich;
 
 
    public RechnerGUI()
    {
        // Fenster-Layout
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            System.out.println("Error setting native LAF: " + e);
        }
 
        this.setTitle("Rechner");
        this.setSize (260,260);
        this.setResizable(false);
        this.setDefaultCloseOperation (EXIT_ON_CLOSE);
 
        contentPane = getContentPane();
        contentPane.setBackground(RECHNER_GRAY);
      //  contentPane.setLayout(new GridLayout(1,1));
 
        // Menue erstellen
         menuBar = new JMenuBar();
         menuBearbeiten = new JMenu("Berarbeiten");
         menuBearbeiten.setMnemonic(java.awt.event.KeyEvent.VK_B);
         menuAnsicht = new JMenu("Ansicht");
         menuAnsicht.setMnemonic(java.awt.event.KeyEvent.VK_A);
         menuHilfe = new JMenu("?");
         menuHilfe.setMnemonic(java.awt.event.KeyEvent.VK_H);
         menuItemKopieren = new JMenuItem("Kopieren Strg+C");
         menuItemKopieren.setMnemonic(java.awt.event.KeyEvent.VK_K);
         menuItemEinfuegen = new JMenuItem("Einfügen Strg+V");
         menuItemEinfuegen.setMnemonic(java.awt.event.KeyEvent.VK_E);
         menuBearbeiten.add(menuItemKopieren);
         menuBearbeiten.add(menuItemEinfuegen);
         menuItemStandard = new JRadioButtonMenuItem("Standard");
         menuItemStandard.setMnemonic(java.awt.event.KeyEvent.VK_S);
         menuItemWissenschaftlich = new JRadioButtonMenuItem("Wissenschaftlich");
         menuItemWissenschaftlich.setMnemonic(java.awt.event.KeyEvent.VK_W);
         menuItemZifferngruppierung = new JRadioButtonMenuItem("Zifferngruppierung");
         menuItemZifferngruppierung.setMnemonic(java.awt.event.KeyEvent.VK_Z);
         bgAnsicht = new ButtonGroup();
         bgAnsicht.add(menuItemStandard);
         menuItemStandard.setSelected(true);
         bgAnsicht.add(menuItemWissenschaftlich);
         bgAnsicht.add(menuItemZifferngruppierung);
         menuAnsicht.add(menuItemStandard);
         menuAnsicht.add(menuItemWissenschaftlich);
         menuAnsicht.addSeparator();
         menuAnsicht.add(menuItemZifferngruppierung);
         menuItemHilfethemen = new JMenuItem("Hilfethemen");
         menuItemHilfethemen.setMnemonic(java.awt.event.KeyEvent.VK_T);
         menuItemInfo = new JMenuItem("Info");
         menuItemInfo.setMnemonic(java.awt.event.KeyEvent.VK_I);
         menuHilfe.add(menuItemHilfethemen);
         menuHilfe.addSeparator();
         menuHilfe.add(menuItemInfo);
         menuBar.add(menuBearbeiten);
         menuBar.add(menuAnsicht);
         menuBar.add(menuHilfe);
         setJMenuBar(menuBar);
 
 
         JPanel panel1 = new JPanel(new GridLayout(1,1,0,50));
         JPanel panel2 = new JPanel(new BorderLayout());
         JPanel panel3 = new JPanel(new GridLayout(5,1));
 
         JPanel panel4 = new JPanel(new BorderLayout());
         JPanel panel5 = new JPanel(new GridLayout(1,3,5,5));
         JPanel panel6 = new JPanel(new GridLayout(4,5,5,8));
         panel4.add(panel5,BorderLayout.NORTH);
         panel4.add(panel6,BorderLayout.CENTER);
         panel2.add(panel1,BorderLayout.NORTH);
         panel2.add(panel3,BorderLayout.WEST);
         panel2.add(panel4,BorderLayout.CENTER);
        // Erzeugen der Anzeige
          tfDisplay = new JTextField("0");
          tfDisplay.setSize(20,10);
          tfDisplay.setEditable(true);
          tfDisplay.setBackground(Color.WHITE);
          tfDisplay.setHorizontalAlignment(JTextField.RIGHT);
          tfDisplay.setFont(RECHNER_FONT);
          panel1.add(tfDisplay);
          //contentPane.add(panel1);
          contentPane.add(panel2);
 
        // Erzeugen der Buttons
           tfM = new JTextField();
           tfM.setEditable(false);
           tfM.setBackground(RECHNER_GRAY);
           tfM.setHorizontalAlignment(JTextField.RIGHT);
           tfM.setFont(RECHNER_FONT);
           tfM.setSize(10,10);
           buttonMC = new JButton("MC");
           buttonMC.setForeground(Color.RED);
           buttonMC.setMargin(RECHNER_MARGIN_VERTICAL);
           buttonMC.setFont(RECHNER_FONT);
           buttonMR = new JButton("MR");
           buttonMR.setForeground(Color.RED);
           buttonMR.setMargin(RECHNER_MARGIN_VERTICAL);
           buttonMR.setFont(RECHNER_FONT);
           buttonMS = new JButton("MS");
           buttonMS.setForeground(Color.RED);
           buttonMS.setMargin(RECHNER_MARGIN_VERTICAL);
           buttonMS.setFont(RECHNER_FONT);
           buttonMPlus = new JButton("M+");
           buttonMPlus.setForeground(Color.RED);
           buttonMPlus.setMargin(RECHNER_MARGIN_VERTICAL);
           buttonMPlus.setFont(RECHNER_FONT);
           panel3.add(tfM);
           panel3.add(buttonMC);
           panel3.add(buttonMR);
           panel3.add(buttonMS);
           panel3.add(buttonMPlus);
 
        // waagrechte Schalterleiste oben
           buttonRuecktaste = new JButton("Rücktaste");
           buttonRuecktaste.setForeground(Color.RED);
           buttonRuecktaste.setMargin(RECHNER_MARGIN_HOR);
           buttonRuecktaste.setFont(RECHNER_FONT);
           buttonCE = new JButton("CE");
           buttonCE.setForeground(Color.RED);
           buttonCE.setMargin(RECHNER_MARGIN_HOR);
           buttonCE.setFont(RECHNER_FONT);
           buttonC = new JButton("C");
           buttonC.setForeground(Color.RED);
           buttonC.setMargin(RECHNER_MARGIN_HOR);
           buttonC.setFont(RECHNER_FONT);
           panel5.add(buttonRuecktaste);
           panel5.add(buttonCE);
           panel5.add(buttonC);
 
           button7 = new JButton("7");
           button7.setMaximumSize(new Dimension(5,5));
           button8 = new JButton("8");
           button9 = new JButton("9");
           buttonDiv = new JButton("/");
           buttonSqrt = new JButton("sqrt");
           button4 = new JButton("4");
           button5 = new JButton("5");
           button6 = new JButton("6");
           buttonMul = new JButton("*");
           buttonProzent = new JButton("%");
           button1 = new JButton("1");
           button2 = new JButton("2");
           button3 = new JButton("3");
           buttonMinus = new JButton("-");
           buttonKehrwert = new JButton("1/x");
           button0 = new JButton("0");
           buttonPlusMinus = new JButton("+/-");
           buttonKomma = new JButton(",");
           buttonPlus = new JButton("+");
           buttonGleich = new JButton("=");
 
        // unterer Schalterblock Zeile 1
           panel6.add(button7);
           panel6.add(button8);
           panel6.add(button9);
           panel6.add(buttonDiv);
           panel6.add(buttonSqrt);
        // unterer Schalterblock Zeile 2
           panel6.add(button4);
           panel6.add(button5);
           panel6.add(button6);
           panel6.add(buttonMul);
           panel6.add(buttonProzent);
        // unterer Schalterblock Zeile 3
           panel6.add(button1);
           panel6.add(button2);
           panel6.add(button3);
           panel6.add(buttonMinus);
           panel6.add(buttonKehrwert);
        // unterer Schalterblock Zeile 4
           panel6.add(button0);
           panel6.add(buttonPlusMinus);
           panel6.add(buttonKomma);
           panel6.add(buttonPlus);
           panel6.add(buttonGleich);
        // ActionListener erzeugen und an die Buttons binden
           al9 = new ActionListener9();
           button9.addActionListener(al9);
           al8 = new ActionListener8();
           button8.addActionListener(al8);
           al7 = new ActionListener7();
           button7.addActionListener(al7);
           al6 = new ActionListener6();
           button6.addActionListener(al6);
           al5 = new ActionListener5();
           button5.addActionListener(al5);
           al4 = new ActionListener4();
           button4.addActionListener(al4);
           al3 = new ActionListener3();
           button3.addActionListener(al3);
           al2 = new ActionListener2();
           button2.addActionListener(al2);
           al1 = new ActionListener1();
           button1.addActionListener(al1);
           al0 = new ActionListener0();
           button0.addActionListener(al0);
           alMC = new ActionListenerMC();
           buttonMC.addActionListener(alMC);
           alMR = new ActionListenerMR();
           buttonMR.addActionListener(alMR);
           alMS = new ActionListenerMS();
           buttonMS.addActionListener(alMS);
           alMPlus = new ActionListenerMPlus();
           buttonMPlus.addActionListener(alMPlus);
           alRuecktaste = new ActionListenerRuecktaste();
           buttonRuecktaste.addActionListener(alRuecktaste);
           alCE = new ActionListenerCE();
           buttonCE.addActionListener(alCE);
           alC = new ActionListenerC();
           buttonC.addActionListener(alC);
           alDiv = new ActionListenerDiv();
           buttonDiv.addActionListener(alDiv);
           alPlus = new ActionListenerPlus();
           buttonPlus.addActionListener(alPlus);
           alMinus = new ActionListenerMinus();
           buttonMinus.addActionListener(alMinus);
           alMul = new ActionListenerMul();
           buttonMul.addActionListener(alMul);
           alSqrt = new ActionListenerSqrt();
           buttonSqrt.addActionListener(alSqrt);
           alProzent = new ActionListenerProzent();
           buttonProzent.addActionListener(alProzent);
           alKehrwert = new ActionListenerKehrwert();
           buttonKehrwert.addActionListener(alKehrwert);
           alGleich = new ActionListenerGleich();
           buttonGleich.addActionListener(alGleich);
           alKomma = new ActionListenerKomma();
           buttonKomma.addActionListener(alKomma);
           alPlusMinus = new ActionListenerPlusMinus();
           buttonPlusMinus.addActionListener(alPlusMinus);
 
    }
 
    public static void main (String [] args) {
        RechnerGUI myFrame = new RechnerGUI();
        myFrame.setVisible(true);
    }
 
    // innere Listener-Klassen
 
    // ActionListener
    class ActionListenerMC implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("MC");
        }
    }
 
    class ActionListenerMR implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("MR");
        }
    }
 
    class ActionListenerMS implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("MS");
        }
    }
 
    class ActionListenerMPlus implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("M+");
        }
    }
 
    class ActionListenerRuecktaste implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("Rücktaste");
        }
    }
 
    class ActionListenerCE implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("CE");
        }
    }
 
    class ActionListenerC implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("C");
        }
    }
 
    class ActionListener7 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("7");
        }
    }
 
    class ActionListener8 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("8");
        }
    }
 
    class ActionListener9 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("9");
        }
    }
 
    class ActionListenerDiv implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("/");
        }
    }
 
    class ActionListenerSqrt implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("sqrt");
        }
    }
 
    class ActionListener4 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("4");
        }
    }
 
    class ActionListener5 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("5");
        }
    }
 
    class ActionListener6 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("6");
        }
    }
 
    class ActionListenerMul implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("*");
        }
    }
 
    class ActionListenerProzent implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("%");
        }
    }
 
    class ActionListener1 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("1");
        }
    }
 
    class ActionListener2 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("2");
        }
    }
 
    class ActionListener3 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("3");
        }
    }
 
    class ActionListenerMinus implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("-");
        }
    }
 
    class ActionListenerKehrwert implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("1/x");
        }
    }
 
    class ActionListener0 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("0");
        }
    }
 
    class ActionListenerPlusMinus implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("+/-");
        }
    }
 
    class ActionListenerKomma implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText(",");
        }
    }
 
    class ActionListenerPlus implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("+");
        }
    }
 
    class ActionListenerGleich implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            tfDisplay.setText("=");
        }
    }
 
 
}
Pour l'instant, les calculs ne sont pas importants mais plutot l'apparence. Le truc est je n'arrive pas avec BorderLayout et GridLayout à produire exactement la meme interface que celle de windows(il ya des problèmes de bordures et mes boutons sont un peu plus gros, certains textes ne s'affichent pas). Si vous lancez calc de windows et comparer avec la mienne vous comprendrez où est le problème. Je ne dois qu'utiliser les 2 types de layouts cités en haut (contraintes du devoir).

Merci pour toute contribution.

Bonne journée