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
|
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Evenement extends JFrame implements ActionListener{
private JPanel pan=null;
private FlowLayout lay=null;
private JButton b=null;
private JTextField t1=null;
private JTextField t2=null;
private JLabel lab1=null;
private JLabel lab2=null;
public Evenement()
{
super();
build();
}
private void build()
{
this.setTitle("ma premiere application");
this.setSize(260,120);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setContentPane(getContents());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private JPanel getContents()
{
lay=new FlowLayout();
//lay.setAlignment(FlowLayout.LEFT);
pan=new JPanel();
pan.setLayout(lay);
t1=new JTextField();
t1.setPreferredSize(new Dimension(70,25) );
pan.add(t1);
lab1=new JLabel("+");
lab1.setPreferredSize(new Dimension(20,25) );
pan.add(lab1);
t2=new JTextField();
t2.setPreferredSize(new Dimension(70,25) );
pan.add(t2);
b=new JButton("Calculer");
b.setPreferredSize(new Dimension(170,25) );
pan.add(b);
lab2=new JLabel("Resultat = ");
lab2.setPreferredSize(new Dimension(150,25) );
pan.add(lab2);
return pan;
}
public static void main(String[] args) {
Evenement e=new Evenement();
e.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b)
{
String n1 = t1.getText();//On récupère la valeur dans le premier champ
int nombre1 = Integer.parseInt(n1);//On convertit cette valeur en un nombre
String n2 = t2.getText();//On récupère la valeur dans le deuxième champ
int nombre2 = Integer.parseInt(n2);//On convertit cette valeur en un nombre
int resultat = nombre1 + nombre2;
lab2.setText("Résultat = " + resultat);
}
}
} |
Partager