import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; 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.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField; public class Test extends JFrame{ // Nombre choisi double n; // Resultat double factorielle = 1; int compteur = 1; JPanel panel = new JPanel(); JPanel JP1 = new JPanel(); private JTextField jtf = new JTextField(); private JLabel label1 = new JLabel("Nombre n"); private JLabel label2 = new JLabel("Factorielle: "); private JButton bouton = new JButton("Calculer la factorielle"); // Barre de menu private JMenuBar menuBar = new JMenuBar(); private JMenu Fichier = new JMenu("Fichier"); private JMenu Aide = new JMenu("Aide"); private JMenuItem item1 = new JMenuItem("Manuel"); private JMenuItem item2 = new JMenuItem("Quitter"); public Test() { // Parametres de la fenetre this.setTitle("Calcul de factorielle"); this.setSize(500, 150); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setLocationRelativeTo(null); this.Fichier.add(item2); this.menuBar.add(Fichier); this.Aide.add(item1); this.menuBar.add(Aide); // Clic sur "Quitter" item2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { System.exit(0); } }); // Clic sur "Mode d'emploi" item1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { Runtime runtime = Runtime.getRuntime(); Process process = null; try { process = runtime.exec(new String[]{"open", "./Manuel.pdf"}); } catch(Exception err) { System.out.println("err = " + err); } } }); panel.setBackground(Color.WHITE); panel.setLayout(new BorderLayout()); // Parametres des caracteres du champ de texte Font police = new Font("Arial", Font.BOLD, 14); jtf.setFont(police); jtf.setPreferredSize(new Dimension(150, 30)); jtf.setForeground(Color.BLUE); // Fixation des objets sur la fenetre JP1.add(label1); JP1.add(jtf); JP1.add(label2); panel.add(JP1, BorderLayout.NORTH); panel.add(bouton); // Pression sur le bouton => effectuer la class BoutonListener bouton.addActionListener(new BoutonListener()); this.setJMenuBar(menuBar); this.setContentPane(panel); this.setVisible(true); } class BoutonListener implements ActionListener{ public void actionPerformed(ActionEvent arg0) { // Recuperation du nombre entre dans le champ de texte String text = ""; try{ text = jtf.getText(); // Enregistrement du nombre dans la variable "n" n = Double.valueOf(text).doubleValue(); // Calcul while (compteur <= n) { factorielle = factorielle * compteur; compteur = compteur + 1; } label2.setText("Factorielle: " + factorielle); label2.setForeground(Color.RED); } catch(NumberFormatException erreur){ label2.setText("Entrer un nombre!"); label2.setForeground(Color.RED); } } } public static void main(String[] args) { Test test = new Test(); } }