Bonjour, alors voici mon problème. Je fais un programme qui permet d'écrire un chiffre compris entre 0 et 20 dans un JTextField, cependant je ne sais pas comment faire pour que la valeur entrée soit l'envoyez en appuyant sur le bouton.
Et ensuite pouvoir recupère la valeur envoyez au bouton par exemple pour afficher ("Vous avez selectionnez le chiffre 1").
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 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 java.text.NumberFormat; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Fenetre extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel container = new JPanel(); private JLabel label = new JLabel("Valeur entre 0 et 20"); private JFormattedTextField jtf = new JFormattedTextField(NumberFormat.getIntegerInstance()); private JButton b = new JButton ("OK"); int NbrSelect; public Fenetre(){ this.setTitle("Animation"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); JPanel top = new JPanel(); Font police = new Font("Arial", Font.BOLD, 14);//Selection de police d'écriture + taille jtf.setFont(police); jtf.setPreferredSize(new Dimension(150, 30)); //tAILLE DE jtEXTFILD jtf.setForeground(Color.RED); b.addActionListener(new ActionListener()); top.add(label); top.add(jtf); //Ajoute le TextField top.add(b); //Ajoute le bouton OK. container.add(top, BorderLayout.NORTH); this.setContentPane(container); this.setVisible(true); container.setBackground(Color.WHITE); //Couleur de fond container.setLayout(new BorderLayout()); int i = 0; try { i = Integer.parseInt(jtf.getText()); } catch(NumberFormatException nfe) { // pas un entier } // String i = jtf.getText(); //i = Integer.parseInt(jtf.getText()); if( i == 1){System.out.println("joeur tu bois");} //Classe écoutant notre premier bouton class BoutonListener implements ActionListener{ //Redéfinition de la méthode actionPerformed() @Override public void actionPerformed(ActionEvent e) { } } } }
Mon main
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 import java.sql.SQLException; import javax.swing.JFrame; public class main { public static void main( String [] args ) throws SQLException { JFrame f = new Fenetre(); } }
Partager