2016-11-13 2 views
-1

Quelqu'un peut-il m'aider? Ma question est comment puis-je imprimer toute la sortie dans un champ de texte?Impression sur le champ de texte

System.out.print("Enter sentence"); 
word = input.nextLine(); 
word= word.toUpperCase(); 

String[] t = word.split(" "); 

for (String e : t) 
    if(e.startsWith("A")){ 

     JFrame f= new JFrame ("Output"); 
     JLabel lbl = new JLabel (e); 

     f.add(lbl); 
     f.setSize(300,200); 
     f.setVisible(true); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 
+0

Créer une 'JTextField' et utiliser la méthode' setText (texte); ' – GOXR3PLUS

Répondre

0

Le code affiché fera des choses folles: vous créez et montrant un nouveau cadre chaque fois que vous trouvez une chaîne commençant par « A ». Vous devez créer le cadre juste une fois, par exemple vous pouvez préparer la sortie avant de créer le cadre en concaténant les éléments souhaités du tableau 't'. Ensuite, vous créez le cadre en ajoutant un JTextField avec le texte souhaité. Vous préférerez peut-être imprimer la sortie dans différentes lignes dans une zone de texte. Si vous utilisez une zone de texte, vous pouvez également utiliser la méthode append (voir l'exemple ci-dessous).

Rappelez-vous que

setVisible(true) 

devrait être la dernière instruction lorsque vous créez un JFrame, ou vous pourriez avoir un comportement indésirable, surtout si vous essayez d'ajouter des composants au châssis après l'appeler. De même, il est préférable d'appeler la méthode pack() sur le JFrame plutôt que de définir la taille manuellement.

Regardez l'exemple ci-dessous pour voir comment vous pouvez résoudre votre problème:

import java.util.Scanner; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
public class Test 
{ 
    public static void main(String[] a) { 
     System.out.print("Enter sentence"); 
     Scanner input = new Scanner(System.in); 
     String word = input.nextLine().toUpperCase(); 
     String[] t = word.split(" "); 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       String separator = System.lineSeparator(); // decide here the string used to separate your output, you can use "" if you don't want to separate the different parts of the array 't' 
       JFrame f = new JFrame ("Output"); 
       // First solution: use a JTextArea 
       JTextArea textArea = new JTextArea(); 
       textArea.setEditable(false); // you might want to turn off the possibilty to change the text inside the textArea, comment out this line if you don't 
       textArea.setFocusable(false); // you might want to turn off the possibility to select the text inside the textArea, comment out this line if you don't 
       for (String e : t) if(e.startsWith("A")) textArea.append(e + separator); 
       f.add(textArea); 
       // Second solution: If you still want to have all the output into a textfield, replace the previous 5 lines of code with the following code : 
       // separator = " "; // in this case don't use a newline to separate the output 
       // String text = ""; 
       // for (String e : t) if(e.startsWith("A")) text += e + separator; 
       // f.add(new JTextField(text)); 
       // f.setSize(300,200); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.pack(); 
       f.setLocationRelativeTo(null); 
       f.setVisible(true); 
      } 
     }); 
    } 
}