Ajouter un commentaire

ezo-esm

Bonjour markove

pour l'application calculatrice voila un code j'espere que ça t'aidera

import java.awt.*;

import javax.swing.*;
import java.awt.event.*;

class Calculatrice extends JFrame implements ActionListener {

	JButton b1 = new JButton("1");
	JButton b2 = new JButton("2");
	JButton b3 = new JButton("3");
	JButton b4 = new JButton("4");
	JButton b5 = new JButton("5");
	JButton b6 = new JButton("6");
	JButton b7 = new JButton("7");
	JButton b8 = new JButton("8");
	JButton b9 = new JButton("9");
	JButton bV = new JButton(",");
	JButton b0 = new JButton("0");
	JButton bE = new JButton("=");
	JButton bP = new JButton("+");
	JButton bM = new JButton("-");
	JButton bF = new JButton("*");
	JButton bD = new JButton("/");
	JButton bO = new JButton("Off");
	JButton bC = new JButton(" C ");

	JTextField resultat = new JTextField(20);
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	JPanel p3 = new JPanel();
	String n1 = "";
	String n2 = "";
	int somme = 0;
	int a;

	public Calculatrice(String title) {

		super(title);
		setSize(400, 300);
		setBackground(Color.black);
		b1.setBackground(Color.red);

		b2.setBackground(Color.red);
		b3.setBackground(Color.red);
		b4.setBackground(Color.red);
		b5.setBackground(Color.red);
		b6.setBackground(Color.red);
		b7.setBackground(Color.red);
		b8.setBackground(Color.red);
		b9.setBackground(Color.red);
		b0.setBackground(Color.red);
		bE.setBackground(Color.red);
		bD.setBackground(Color.red);
		bV.setBackground(Color.red);
		bM.setBackground(Color.red);
		bF.setBackground(Color.red);
		bE.setBackground(Color.red);
		bP.setBackground(Color.red);
		bO.setBackground(Color.red);

		p1.setLayout(new GridLayout(4, 4, 20, 20));
		b1.addActionListener(this);
		p1.add(b1);
		b2.addActionListener(this);
		p1.add(b2);
		b3.addActionListener(this);
		p1.add(b3);
		bP.addActionListener(this);
		p1.add(bP);
		b4.addActionListener(this);
		p1.add(b4);
		b5.addActionListener(this);
		p1.add(b5);
		b6.addActionListener(this);
		p1.add(b6);
		bM.addActionListener(this);
		p1.add(bM);
		b7.addActionListener(this);
		p1.add(b7);
		b8.addActionListener(this);
		p1.add(b8);
		b9.addActionListener(this);
		p1.add(b9);
		bF.addActionListener(this);
		p1.add(bF);
		bV.addActionListener(this);
		p1.add(bV);
		b0.addActionListener(this);
		p1.add(b0);
		bE.addActionListener(this);
		p1.add(bE);
		bD.addActionListener(this);
		p1.add(bD);

		p1.setBackground(Color.red);
		add(p1, "Center");

		p2.setBackground(Color.gray);
		p2.setLayout(new FlowLayout(FlowLayout.CENTER));
		resultat.setEnabled(false);
		resultat.setForeground(Color.gray);
		resultat.setFont(new Font("Serif", Font.ITALIC, 25));
		bC.setForeground(Color.gray);
		bC.setFont(new Font("Serif", Font.ITALIC, 25));
		bC.addActionListener(this);
		bO.setFont(new Font("Serif", Font.ITALIC, 25));
		p2.add(bC);
		p2.add(resultat);
		bO.setBackground(Color.gray);
		bC.setBackground(Color.gray);
		p2.add(bO);
		add(p2, "North");
		JLabel k = new JLabel("la plus belle des calculatrices ", 0);
		p3.add(k);
		p3.setBackground(Color.black);
		add(p3, "South");
		bO.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		getInsets();
		setVisible(true);
	}

	public Calculatrice() {
		// TODO Auto-generated constructor stub
	}

	public Insets getInsets() {
		return new Insets(20, 20, 20, 20);

	}

	public void actionPerformed(ActionEvent arg) {

		try {

			if (arg.getSource() == bO) {
				setDefaultLookAndFeelDecorated(false);
			} else if (arg.getSource() == b1) {
				n1 += "1";
				resultat.setText(n1);
			} else if (arg.getSource() == b2) {
				n1 += "2";
				resultat.setText(n1);
			} else if (arg.getSource() == b3) {
				n1 += "3";
				resultat.setText(n1);
			} else if (arg.getSource() == b4) {
				n1 += "4";
				resultat.setText(n1);
			} else if (arg.getSource() == b5) {
				n1 += "5";
				resultat.setText(n1);
			} else if (arg.getSource() == b6) {
				n1 += "6";
				resultat.setText(n1);
			} else if (arg.getSource() == b7) {
				n1 += "7";
				resultat.setText(n1);
			} else if (arg.getSource() == b8) {
				n1 += "8";
				resultat.setText(n1);
			} else if (arg.getSource() == b9) {
				n1 += "9";
				resultat.setText(n1);
			} else if (arg.getSource() == b0) {
				n1 += "0";
				resultat.setText(n1);
			} else if (arg.getSource() == bV) {
				n1 += ".";
				resultat.setText(n1);
			} else if (arg.getSource() == bP) {
				a = 1;
				somme += Integer.valueOf(n1);
				n1 = "";
				resultat.setText(String.valueOf(somme));
			} else if (arg.getSource() == bM) {
				a = 2;
				somme -= Integer.valueOf(n1);
				n1 = "";
				resultat.setText(String.valueOf(somme));
			} else if (arg.getSource() == bF) {
				a = 3;

				somme *= Integer.valueOf(n1);
				n1 = "";
				resultat.setText(String.valueOf(somme));
			} else if (arg.getSource() == bD) {
				a = 4;
				somme += Integer.valueOf(n1);
				n1 = "";
				resultat.setText(String.valueOf(somme));
			} else if (arg.getSource() == bE) {
				if (a == 1) {

					resultat.setText(String.valueOf(somme
							+ Integer.parseInt(n1)));
				} else if (a == 2) {
					resultat.setText(String.valueOf(somme
							- Integer.parseInt(n1)));
				} else if (a == 3) {
					resultat.setText(String.valueOf(somme
							* Integer.parseInt(n1)));
				} else if (a == 4) {
					int k = Integer.parseInt(n1);
					if (k == 0) {
						JOptionPane.showMessageDialog(this,
								" division invalide par 0!!");
					} else
						resultat.setText(String.valueOf(somme / k));
				}

			} else if (arg.getSource() == bC) {
				resultat.setText("");
				n1 = "";
				somme = 0;
			}

		} catch (Exception m) {
			JOptionPane.showMessageDialog(this, "erreur");
		}

	}

	public static void main(String[] u) {
		Calculatrice c = new Calculatrice();
		c.setVisible(true);

	}

}

je ne dis pas que c'est juste mais j'espere
bonne chance

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 L     ZZZZZ  EEEE  K  K  H  H 
L Z E K K H H
L Z EEE KK HHHH
L Z E K K H H
LLLL ZZZZZ EEEE K K H H