Pages

Tuesday, February 3, 2015

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

public class calculette implements ActionListener
{


JFrame f = new JFrame("Calculatrice" );

JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JButton btn1=new JButton("+");
JButton btn2=new JButton("-");
JButton btn3=new JButton("*");
JButton btn4=new JButton("/");
JLabel res=new JLabel();




JLabel lbl1=new JLabel("Nombre1");
JTextField txt1=new JTextField(10);
JLabel lbl2=new JLabel("Nombre2");
JTextField txt2=new JTextField(10);
public calculette()
{
p1.add(lbl1);p1.add(txt1);
p1.add(lbl2);p1.add(txt2);
p2.add(btn1);p2.add(btn2);
p2.add(btn3);p2.add(btn4);
p3.add(res);
f.add(p1, BorderLayout.NORTH);f.add(p2,BorderLayout.CENTER);f.add(p3,BorderLayout.SOUTH);

btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
f.setBounds(400, 400, 400, 400);
f.setVisible(true);
}

public int add(int a,int b){
int res=a+b;
return res;
}


public int sous(int a,int b){
int res=a-b;
return res;
}


public int div(int a,int b){
int res=a/b;
return res;
}

public int mult(int a,int b){
int res=a*b;
return res;
}
public void actionPerformed(ActionEvent evt)
{
String op = evt.getActionCommand();
int a=Integer.parseInt(txt1.getText());
int b=Integer.parseInt(txt2.getText());

switch (op) {
case "+":
res.setText("resultat= "+(a+b));
break;
case "-":
res.setText("resultat= "+(a-b));
break;
case "*":
res.setText("resultat= "+(a*b));
break;
case "/":
res.setText("resultat= "+(a/b));
break;
default:
break;
}

}

public static void main(String[] args) {
new calculette();
}
}

No comments:

Post a Comment