我需要设置三个一维数组来包含等效的英语和其他语言的你选择的名词,以及这些对象的图像。当我打开小程序时,根本没有任何事情发生。我不确定我的编码有多接近。我真的不知道如何实现这一目标,但这就是我想出来的。我还没有添加任何图片。到目前为止,这是我的代码。
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonArray extends JApplet implements ActionListener
{
JButton button;
JTextField textfield;
String textFieldInput = "Enter a word from the dictionary";
int fromString;
String[] english={"hambuger", "pen", "house"};
String[] spanish={"hamburguesa", "boligrafo", "casa"};
String[] picture={"image1", "image2", "image3"};
public static void main(String[] args){
String[] english={"hambuger", "pen", "house"};
String[] spanish={"hamburguesa", "boligrafo", "casa"};
String[] picture={"image1", "image2", "image3"};
Scanner scan = new Scanner(System.in);
System.out.println("English word to translate: ");
String temp = scan.next();
}
public void init() {
Scanner scan = new Scanner(System.in);
System.out.println("English word to translate: ");
String temp = scan.next();
this.setLayout(null);
textfield = new JTextField("Enter a word from the dictionary");
this.add(textfield);
textfield.setBounds(10, 10, 400, 20);
button = new JButton("Translate");
this.add(button);
button.addActionListener(this);
button.setBounds(50, 50, 150, 20);
}
public void actionPerformed(ActionEvent event) {
Scanner scan = new Scanner(System.in);
String temp = scan.next();
for(int i=0; i < english.length; i++){
if(temp == english[i]){
System.out.println(spanish[i]);
System.out.println(picture[i]);
}
if(i==english.length || temp != english[i]){
System.out.println("Word is not found");
}
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawString("hamburger", 300, 250);
g.drawString("pen", 300, 270);
g.drawString("house", 300, 290);
g.drawString("hamburguesa", 300, 250);
g.drawString("boligrafo", 320, 270);
g.drawString("casa", 340, 290);
}
}