Bonjour à tous,

J'ai déjà sollicité votre aide sur ce forum et elle fut superbe.

J'ai quasiment fini mon projet informatique, un calculette basique en JAVA.
Le problème, c'est que pour faire un simple calcul ( nombre1 + nombre2 ), il faut "stocker" ces valeurs pour faire le calcul.

Là est mon problème, lorsque j'appuie sur ma touche "=", le résultat est "0.0"

En réalité, lorsque je stock pour la premiere fois "nombre1", dès que je quitte la fonction pour connaitre l'opérateur cliqué (+ - * /) puis le second nombre, ces informations sont déjà oubliées.

Je crois que cela réagit de cette manière car j'utilise des statics pour mes fonctions ce qui me permet de ne pas instancier un objet.
J'ai déjà essayé d'instancier mais le résultat fut affreux lol.

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
 
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
 
 
public class My_Window extends JFrame {
 
	JLabel screen = new JLabel();
	JPanel container = new JPanel();
	JPanel operateurs = new JPanel();
	JPanel numbers = new JPanel();
	JPanel panScreen = new JPanel();
 
	public My_Window()
	{
	    this.setTitle("Simple Count");
	    this.setSize(300, 400);
	    this.setLocationRelativeTo(null);
	    this.setResizable(false);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//this.container.setBackground(Color.);
	    this.setContentPane(container);
	    this.setVisible(true);
	}
 
	public void Panel_Police()
	{
	    Font police = new Font("Arial", Font.BOLD, 30);
 
	    this.screen.setFont(police);
	    this.screen.setHorizontalAlignment(JLabel.CENTER);
	    this.screen.setPreferredSize(new Dimension(200, 300));
	    this.screen.setHorizontalAlignment(JLabel.RIGHT);
	    this.screen.setPreferredSize(new Dimension(220, 20));
 
	    this.operateurs.setPreferredSize(new Dimension(55, 225));
	    this.numbers.setPreferredSize(new Dimension(220, 300));
	    this.panScreen.setPreferredSize(new Dimension(220, 50));
	}
 
	public void My_Buttons()
	{
		String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0"};
		String[] pad = {"C", "=", "+", "-", "*", "/"};
		JButton[] nbButtons = new JButton[numbers.length];
		JButton[] opButtons = new JButton[pad.length];
 
		int i = 0;
	    while (numbers.length != i)
	    {
	    	nbButtons[i] = new JButton(numbers[i]);
	    	this.numbers.add(nbButtons[i]);
	    	nbButtons[i].addActionListener(new Listeners.nbListener(this.screen));
	    	nbButtons[i].setPreferredSize(new Dimension(60, 50));
	    	i++;
	    }
	    i = 0;
	    while (pad.length != i)
	    {
	    	opButtons[i] = new JButton(pad[i]);
	    	if (pad[i] == "C")
	    		this.operateurs.add(opButtons[i]).setForeground(Color.red);
	    	else
	    		this.operateurs.add(opButtons[i]);
	    	opButtons[i].addActionListener(new Listeners.opListener(this.screen));
	    	this.setPreferredSize(new Dimension(200, 100));
	    	i++;
	    }
 
	}
 
	public void Add_to_container()
	{
		this.panScreen.add(screen);
	    this.panScreen.setBorder(BorderFactory.createLineBorder(Color.black));
	    this.container.add(panScreen, BorderLayout.NORTH);
	    panScreen.setBackground(Color.WHITE);
	    this.container.add(numbers, BorderLayout.CENTER);
	    this.container.add(operateurs, BorderLayout.EAST);		
	}
 
}
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
 
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JLabel;
 
import java.awt.event.ActionEvent;
 
public class Listeners {
 
 
	public static class nbListener implements ActionListener {
		private JLabel screen;
 
		public nbListener(JLabel screen) {
            this.screen = screen;
		}
 
		public void actionPerformed(ActionEvent e) {
			String text = ((JButton) e.getSource()).getText();
			text = screen.getText() + text;
			screen.setText(text);
			}
		}
 
 
	public static class opListener implements ActionListener {
		private JLabel screen;
		private String op;
		private /*static*/  double result;
		private /*static*/ double nb1;
 
		public opListener(JLabel screen) {
			this.screen = screen;
		}
 
		public void actionPerformed(ActionEvent e) {
			String op = ((JButton) e.getSource()).getText();
			if (op == "=")
			{
			//	System.out.print(this.nb1);
				this.result = Calcul.calcul(this.screen, this.op, this.nb1);
			//	System.out.print(result);
				String str = result + "";
				screen.setText(str);
			}
			else
			{
				this.nb1 = Double.valueOf(screen.getText());
				screen.setText("");
			}
		}
	}
 
}
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
 
import javax.swing.JLabel;
 
 
public class Calcul {
	static double result;
 
	public static double calcul(JLabel screen, String op, double nb1) {
		if (op == "+")
			result = nb1 + Double.valueOf(screen.getText());
		else if (op == "-")
			result = nb1 - Double.valueOf(screen.getText());
		else if (op == "*")
			result = nb1 * Double.valueOf(screen.getText());
		else if (op == "/")
			result = nb1 / Double.valueOf(screen.getText());
		return (result);
	}
 
}