2017-07-21 1 views
1

Je dois remplir ce code avec ces mesures simples. Je ne peux rien faire de trop compliqué. Je veux traduire à partir du champ de texte d'entrée supérieur et la sortie sur le champ de texte en bas. Jusqu'à présent, cela semble correct, mais ma traduction sort simplement dans le même champ de texte que mon entrée. Je suis un noob, et vérifié mes notes et manuel, et ne peux pas comprendre comment changer la sortie au champ inférieur. Cela ne semble pas possible avec ce niveau de code. La traduction est juste. Je pense que j'ai besoin de modifier le bouton Traduire, mais je ne sais pas où indiquer quoi. Cela fonctionne bien si je voulais juste sortir dans ma boîte de saisie. Eh bien, voici mon code jusqu'à présent:deux champs de texte, une entrée, une sortie

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 



public class Translator4 extends JFrame implements ActionListener 
{ 


public static final int WIDTH = 500; 
public static final int HEIGHT = 500; 
public static final int NUMBER_OF_CHAR = 50; 

private JTextField phrase; 
private JTextField translatedphrase; 

public static void main(String[] args) 
{ 
    Translator4 gui = new Translator4(); 
    gui.setVisible(true); 
} 

public Translator4() 
{ 
    //title bar and overall size 
    super("Pig Latin Translator v.4.0"); 
    setSize(WIDTH, HEIGHT); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLayout(new GridLayout(3,1)); 

    //create input text filed 
    JPanel namePanel = new JPanel(); 
    namePanel.setLayout(new BorderLayout()); 
    namePanel.setBackground(Color.WHITE); 

    phrase = new JTextField(NUMBER_OF_CHAR); 
    namePanel.add(phrase, BorderLayout.CENTER); 
    JLabel nameLabel = new JLabel("Enter the phrase in English to be translated:"); 
    namePanel.add(nameLabel, BorderLayout.NORTH); 

    add(namePanel); 

    //create the buttons 
    JPanel buttonPanel= new JPanel(); 
    buttonPanel.setLayout(new FlowLayout()); 
    buttonPanel.setBackground(Color.GREEN); 

    JButton actionButton = new JButton("Translate"); 
    actionButton.addActionListener(this); 
    buttonPanel.add(actionButton); 

    JButton clearButton = new JButton("Clear"); 
    clearButton.addActionListener(this); 
    buttonPanel.add(clearButton); 

    add(buttonPanel); 

    //create the output text field  
    JPanel namePanel2 = new JPanel(); 
    namePanel2.setLayout(new BorderLayout()); 
    namePanel2.setBackground(Color.WHITE); 

    translatedphrase = new JTextField(NUMBER_OF_CHAR*2); //output will be larger so I multiplied it by 2 
    namePanel.add(phrase, BorderLayout.CENTER); 
    JLabel nameLabel2 = new JLabel("Translation:"); 
    namePanel2.add(nameLabel2, BorderLayout.NORTH); 

    add(namePanel2); 
} 

public void actionPerformed(ActionEvent e) 
{ 
    String actionCommand = e.getActionCommand(); 



    if (actionCommand.equals("Translate")) //when the user wants a translation, this block executes 
    { 

     String[] words=new String[100]; //takes up to 100 words 
     String sentence = phrase.getText(); //the user input made into a string 
     String newSentence=""; //the output string generated   
     words = sentence.split(" "); //splits based on spaces, no other punctuation allowed 

     for (int index=0; index< words.length; index++) //steps thru the array of words 
     { 
      char firstChar = words[index].charAt(0); //rules for vowels, 'one' becomes 'oneway' 
      if (firstChar=='a'||firstChar=='e'||firstChar=='i'||firstChar=='o'||firstChar=='u') 
      { 
       words[index] = words [index] + "way"; 

       newSentence=newSentence + " " + words[index]; //adds the word just now modified to new sentence 

      } 
      else //rules for words that don't start with vowels, 'blank' becomes 'lankbay' 
      { 
       firstChar = ' '; 
       words[index] = (words[index]).substring(1,(words[index].length())) 
         + (words[index]).charAt(0) + "ay"; 

       newSentence=newSentence + " " + words[index]; //adds the word just now modified to new sentence 
      } 

      phrase.setText(newSentence); //sends the new sentence back for output... problem here 
     } 
    } 

    else if (actionCommand.equals("Clear")) 
     phrase.setText(""); 

    else 
     phrase.setText("Unexpected error."); 
} 


} 
+0

Il renvoie la sortie déjà correcte. Il le place juste dans la même case que l'entrée, en l'écrasant. – GDearth

+0

Comment lier la sortie au champ de texte "traduit" en bas au lieu de simplement l'envoyer dans le champ de texte supérieur? – GDearth

+0

FIXE. Je n'ai pas seulement dû changer la ligne pour: \ n translatedphrase.setText (newSentence); Mais mon deuxième champ a été mal réglé. Je l'ai changé pour: translatedphrase = new JTextField (NUMBER_OF_CHAR * 2); namePanel2.add (translatedphrase, BorderLayout.CENTER); JLabel nameLabel2 = new JLabel ("Translation:"); ; namePanel2.add (nameLabel2, BorderLayout.NORTH); \t Merci pour l'aide. Il m'a fait regarder au bon endroit. – GDearth

Répondre

2

mais ma traduction affiche simplement dans le même champ de texte que mon entrée

C'est parce que ce que vous dites à faire

phrase.setText(newSentence); //sends the new sentence back for output... problem here 

Je suppose, phrase est l'entrée et translatedphrase est la sortie, de sorte que cela signifierait, pour résoudre votre problème immédiat, tout ce que vous devez faire est de r eplace phrase avec translatedphrase

translatedphrase.setText(newSentence); //sends the new sentence back for output... no more problem 

Je vous propose aussi changer les autres setText appels que vous faites contre phrase à translatedphrase ainsi

... Ce

translatedphrase = new JTextField(NUMBER_OF_CHAR * 2); //output will be larger so I multiplied it by 2 
namePanel.add(phrase, BorderLayout.CENTER); 
JLabel nameLabel2 = new JLabel("Translation:"); 
namePanel2.add(nameLabel2, BorderLayout.NORTH); 

est également un problème , comme vous n'ajoutez jamais réellement translatedphrase à quoi que ce soit, vous venez de rajouter phrase à namePanel à nouveau

Donc, je suppose qu'il devrait être

translatedphrase = new JTextField(NUMBER_OF_CHAR * 2); //output will be larger so I multiplied it by 2 
namePanel2.add(translatedphrase, BorderLayout.CENTER); 
JLabel nameLabel2 = new JLabel("Translation:"); 
namePanel2.add(nameLabel2, BorderLayout.NORTH);