I'm trying to make a program that will translate English words to Japanese and vice versa that are listed in the given arrays. It will also display the image for the given word. For my actionPerformed method, I coded it to be when the "English to Japanese" button is pressed the for-loop will run until the input from the JTextField is the same as the words listed in the englishWords array. But I'm not sure if that's right. Is it my for-loop that is incorrect or am I approaching the code wrong?
P.S. ignore the non-completed image code, I'm just trying to figure out how to compare the words together
public class Translator extends JApplet implements ActionListener
{
//English array
String[] englishWords = { "Fish", "Lettuce", "Bike", "Chef", "Teacher" };
//Japanese array
String[] japaneseWords = { "Sakana", "Retasu", "Jitensha", "Shefu", "Sensei" };
//Image array
Image[] wordImages;
Image wrong;
//Java util
JButton english, japanese;
ImageIcon icon;
JLabel label;
JTextField words;
public void init()
{
//Images
wordImages = new Image[5];
wordImages[0] = getImage(getCodeBase(), "Fish.jpg");
wordImages[1] = getImage(getCodeBase(), "Lettuce.jpg");
wordImages[2] = getImage(getCodeBase(), "Bike.jpg");
wordImages[3] = getImage(getCodeBase(), "Chef.jpg");
wordImages[4] = getImage(getCodeBase(), "Sensei.jpg");
wrong = getImage(getCodeBase(), "Wrong.jpg");
//Layout
setLayout(new FlowLayout());
words = new JTextField(null, 10);
english = new JButton("English to Japanese");
japanese = new JButton("Japanese to English");
icon = new ImageIcon(wordImages[0]);
label = new JLabel( icon, JLabel.CENTER);
icon.setImage(wordImages[0]);
label.setIcon(icon);
//ActionListener
english.addActionListener(this);
japanese.addActionListener(this);
//Install
add(words);
add(english);
add(japanese);
add(label);
}
public void actionPerformed( ActionEvent ae )
{
Object src = ae.getSource();
if(src == english)
{
for(int x = 0; x < englishWords.length; ++x)
if(englishWords[x] == words.getText())
icon.setImage(wordImages[x]);
}
else if(src == japanese)
{
for(int x = 0; x < japaneseWords.length; ++x)
if(japaneseWords[x] == words.getText())
icon.setImage(wordImages[x]);
}
}