Bonjour,
Je cherche à écrire du texte dans mon JTextArea à partir d'une méthode.
Alors voilà mon code
Classe TextDemo
Et ma classe de test
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 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TextDemo extends JPanel implements ActionListener { private JTextField textField; private JTextArea textArea; private final static String newline = "\n"; public TextDemo() { super(new GridBagLayout()); textField = new JTextField(40); textField.addActionListener(this); textArea = new JTextArea(15, 40); textArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(textArea); //Add Components to this panel. GridBagConstraints c = new GridBagConstraints(); c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; c.weightx = 1.0; c.weighty = 1.0; add(scrollPane, c); c.fill = GridBagConstraints.HORIZONTAL; add(textField, c); } public void actionPerformed(ActionEvent evt) { String text = textField.getText(); textArea.append(text + newline); textField.selectAll(); //Make sure the new text is visible, even if there //was a selection in the text area. textArea.setCaretPosition(textArea.getDocument().getLength()); } public void addText(String text) { textArea.setText(text); textArea.setCaretPosition(textArea.getDocument().getLength()); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ private static void createAndShowGUI() { //Create and set up the window. JFrame frame = new JFrame("TextDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add contents to the window. frame.add(new TextDemo()); //Display the window. frame.pack(); frame.setVisible(true); } public void run() { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Donc par exemple là, je voudrais que la méthode addText ajoute le texte dans le JTextArea mais il doit manquer quelque chose car ça ne marche pas. Voilà si quelqu'un pouvait m'aider, merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 public class Test { public static void main(String[] args) { TextDemo td = new TextDemo(); td.run(); td.addText("Bonjour"); } }.
Partager