Bonjour, donc j'ai un petit problème que je n'arrive pas résoudre, qui doit être pourtant bien facile...
Donc j'ai créer un programme qui permets de généré un mots de passe selon le nombre de caractère qu'on veut. Donc ensuite je fais un System.out.print(moncode); qui affiche mon mot de passe. Sauf que ce mots de passe je voudrais le récupérer pour le mettre dans un showMessageBox. Merci d'avance !

Mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.Random;
 
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
public class Fenetre extends JFrame { // Fenetre "Panneau"
    /**
         * 
         */
    private static final long serialVersionUID = 1L;
    private final Panneau pan = new Panneau();
    private final JPanel flow = new JPanel();
    private final JButton bouton = new JButton("ok");
    private final JPanel container = new JPanel();
    private final JFormattedTextField jtf = new JFormattedTextField(NumberFormat.getIntegerInstance());
 
    public Fenetre() {
        this.setTitle("Générateur mot de passe");
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
 
        Font police = new Font("Arial", Font.BOLD, 14);
        jtf.setFont(police);
        jtf.setPreferredSize(new Dimension(150, 30)); // tAILLE DE jtEXTFILD
        jtf.setForeground(Color.RED);
        flow.setLayout(new FlowLayout());
        container.setBackground(Color.WHITE);
        container.setLayout(new BorderLayout());
        container.add(pan, BorderLayout.CENTER);
        flow.add(jtf);
        flow.add(bouton);
        container.add(flow, BorderLayout.SOUTH);
        this.setContentPane(container);
        this.setVisible(true);
 
        bouton.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e) {
                int i = 0;
                String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";
                Random rand = new Random();
 
                try {
                    i = Integer.parseInt(jtf.getText());
                } catch (NumberFormatException e2) {
                    // n'est pas un nombre, gérer ce cas
                }
                if (i > 4) {
                    JOptionPane.showMessageDialog(null, "Je chiffre doit être compris entre 1 et 4", "ATTENTION",
                            JOptionPane.ERROR_MESSAGE);
                }
 
                switch (i) {
                    case 1:
                        for (int nb = 0; nb < 4; nb++) {
                            System.out.print(alphabet.charAt(rand.nextInt(alphabet.length())));
 
                        }
                        break;
                    case 2:
                        for (int nb = 0; nb < 6; nb++) {
                            System.out.print(alphabet.charAt(rand.nextInt(alphabet.length())));
                        }
                        break;
                    case 3:
                        for (int nb = 0; nb < 8; nb++) {
                            System.out.print(alphabet.charAt(rand.nextInt(alphabet.length())));
                        }
                        break;
                    case 4:
                        for (int nbi = 0; nbi < 10; nbi++) {
                            System.out.print(alphabet.charAt(rand.nextInt(alphabet.length())));
                        }
                        break;
 
                }
            }
        });
    }