Bonjour,

Je suis débutant en Java et j'aurais bien besoin d'aide.

J'essaye actuellement de réaliser une interface graphique simple avec un Panel et un TextField dans lequel les mots saisis dans le TextField seraient écrit sur le Panel, après un appui sur la touche Enter, puis tomberaient verticalement de haut en bas du Panel.

Pour ce faire j'ai essayé de récupérer le contenu du TextField via une ArrayList, car j'aurais besoin plus tard de stocker tout les mots entrer dans le TextField.

Voila le code que j'ai pour l'instant :
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
93
94
95
96
97
98
99
100
101
102
103
104
<div style="margin-left:40px">import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import java.util.ArrayList;
 
import javax.swing.JPanel;
 
 
public class GameScreen extends JPanel implements ActionListener {
 
ArrayList<Alphabet> listAlphabets = new ArrayList<Alphabet>();    
 
Keyboard tf = new Keyboard();
 
    public GameScreen() {
 
        this.add(tf);
        tf.addActionListener(this);
 
    }
 
    public ArrayList<Alphabet> getListAlphabets() {
 
        return listAlphabets;
 
    }
 
    public void addAlphabet(int x, int y, String name) {
 
        listAlphabets.add(new Alphabet(x, y, name));
 
    }
 
    public void paintComponent(Graphics g) {
 
    g.setColor(Color.white);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
 
        Object[] tab = listAlphabets.toArray();
        for(int i=0; i<tab.length; i++) {
 
            Alphabet a = (Alphabet) tab[i];
            String s = a.getName();
            g.setColor(Color.black);
            g.drawString(s, a.getPosX(), a.getPosY());
 
        } 
    }  
 
    public void actionPerformed(ActionEvent e) {
 
         String s = tf.getText().trim();
         addAlphabet( (int) (Math.random()*this.getWidth()), 0, s);
 
        }
}</div>La classe Keyboard est une extends de JTextField, la mise à jour de mon affichage se fait dans une autre classe que voici :
<div style="margin-left:40px">import java.util.ArrayList;
 
import javax.swing.JFrame;
 
 
public class GameUI extends JFrame {
 
GameScreen screen = new GameScreen();
 
Keyboard tf = new Keyboard();
 
    public GameUI(int height, int width) {
 
        this.setTitle("Attacker Game");
        this.setSize(height, width);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        this.setContentPane(screen);
 
        this.setVisible(true);
 
        update();
 
    }
 
    private void update() {
 
        ArrayList<Alphabet> list = screen.getListAlphabets();
        Object [] tab = list.toArray();
 
        while(true) {
 
            for(int i=0; i<tab.length; i++) {
 
                Alphabet a = (Alphabet) tab[i];
 
                int y = a.getPosY();
                a.setPosY(++y);
 
                screen.repaint();
 
            }
        }
    }
}</div>
Quand j'exécute le programme la fenêtre avec le Panel et le TextField s'affiche mais lorsque je rentre un mot dans le TextField, rien ne se passe.

Merci d'avance pour toute aide ou conseil.

Cordialement,

Julien