【发布时间】:2017-12-27 05:59:20
【问题描述】:
我必须用这些简单的措施来完成这段代码。不能做任何过于复杂的事情。我想从顶部输入文本字段翻译并在底部文本字段输出。到目前为止,它看起来是正确的,但我的翻译只是在与我的输入相同的文本字段中输出。我是菜鸟,检查了我的笔记和教科书,不知道如何将输出更改为底部字段。使用这种级别的代码似乎是不可能的。翻译是对的。我想我需要修改翻译按钮,但不确定在哪里指出什么。如果我只想在我的输入框中输出,它工作正常。好吧,到目前为止,这是我的代码:
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.");
}
}
【问题讨论】:
-
它已经发回了正确的输出。它只是把它和输入放在同一个盒子里,覆盖它。
-
如何将输出绑定到底部的“已翻译”文本字段,而不是将其发送回顶部文本字段?
-
已修复。我不仅必须将行更改为:\n translatephrase.setText(newSentence);但是我的第二个字段设置错误。我将其更改为: translatephrase = new JTextField(NUMBER_OF_CHAR*2); namePanel2.add(translatedphrase, BorderLayout.CENTER); JLabel nameLabel2 = new JLabel("翻译:"); namePanel2.add(nameLabel2, BorderLayout.NORTH);谢谢您的帮助。它让我找到了正确的地方。
标签: swing jpanel jtextfield