两个文本字段,一个输入,一个输出

时间:2017-07-21 08:54:53

标签: swing jpanel jtextfield

我必须使用这些简单的措施完成此代码。不能做任何过于复杂的事情。我想从顶部输入文本字段和底部文本字段输出进行翻译。到目前为止,它看起来是正确的,但我的翻译只输出与我的输入相同的文本字段。我是一个菜鸟,检查了我的笔记和教科书,无法弄清楚如何将输出更改为底部字段。这段代码似乎无法实现。翻译是对的。我想我需要修改翻译按钮,但不知道在哪里指明什么。如果我只想在输入框中输出它,它工作正常。好吧,到目前为止,这是我的代码:

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.");
}


}

1 个答案:

答案 0 :(得分:2)

  

但我的翻译只输出与输入相同的文本字段

那是因为那就是你要告诉它的事情

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

所以我假设,phrase是输入,translatedphrase是输出,这意味着,为了解决您的直接问题,您需要做的就是用{替换phrase {1}}

translatedphrase

我还建议你将translatedphrase.setText(newSentence); //sends the new sentence back for output... no more problem setText的其他phrase来电更改为translatedphrase

此...

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);

也是一个问题,因为您从未真正添加translatedphrase任何内容,只需将phrase重新添加到namePanel

所以,我认为它应该是

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);