package graph3;
import java.awt.*;
public class Calculatrice {
private JFrame frame;
private JTextField zoneInfosOperations;
private int chiffre1,chiffre2,positionChiffre;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calculatrice window = new Calculatrice();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Calculatrice() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
chiffre1=0;
chiffre2=0;
positionChiffre=1; //position du chiffre actuellement affiché
//la fenêtre
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//les boutons
JButton touche1 = new JButton("1");
touche1.setBounds(41, 202, 65, 23);
frame.getContentPane().add(touche1);
(.....)
//les labels
JLabel labelChiffre1 = new JLabel("chiffre1");
labelChiffre1.setBounds(280, 52, 46, 14);
frame.getContentPane().add(labelChiffre1);
JLabel labelChiffre2 = new JLabel("chiffre2");
labelChiffre2.setBounds(280, 68, 46, 14);
frame.getContentPane().add(labelChiffre2);
// la zone texte
zoneInfosOperations = new JTextField();
zoneInfosOperations.setBounds(44, 31, 209, 89);
frame.getContentPane().add(zoneInfosOperations);
zoneInfosOperations.setColumns(10);
touchePlus.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
switch (positionChiffre)
{
case 1:
chiffre1=Integer.parseInt(zoneInfosOperations.getText());
//modification du label
labelChiffre1.setText("hhjhkj");
positionChiffre=2;
break; //indique qu'il faut sortir du switch
case 2:
chiffre2=Integer.parseInt(zoneInfosOperations.getText());
positionChiffre=1;
break; //indique qu'il faut sortir du switch
default: // indication du cas "else"
System.out.println("Erreur: position chiffre anormale");
}
zoneInfosOperations.setText(""); //efface la zone
}
});
toucheEgal.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//zoneInfosOperations.setText(""); //efface la zone
String chaine1="" ;
chaine1= chaine1.valueOf(chiffre1+chiffre2) ; //on transfère la valeur du résultat dans la chaine
zoneInfosOperations.setText(chaine1);
}
});
}
static void effacerZoneOperations()
//note: pour ajouter une méthode à main il faut déclarer "static"
{
}
}
Partager