Class MaFenetre
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MaFenetre extends JFrame implements ActionListener {
public MaFenetre() {
setTitle("Carres");
setSize(400, 100);
Container contenu = getContentPane();
contenu.setLayout(new FlowLayout());
labNombre = new JLabel(etiqNombre);
contenu.add(labNombre);
nombre = new JTextField(10);
contenu.add(nombre);
boutonCalcul = new JButton("CALCUL");
contenu.add(boutonCalcul);
boutonCalcul.addActionListener(this);
labCarre = new JLabel(etiqCarre);
contenu.add(labCarre);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boutonCalcul)
try {
String texte = nombre.getText();
int n = Integer.parseInt(texte);
long carre = (long) n * (long) n;
labCarre.setText(etiqCarre + carre);
} catch (NumberFormatException ex) {
nombre.setText("");
labCarre.setText(etiqCarre);
}
}