Swing GUI 和翻译器问题

3
所以我有一个课程项目,我必须创建一个翻译器,用户可以输入文本(英语),点击按钮,然后获得Pig Latin翻译。我必须使用Swing GUI、FlowLayout和JTextArea,并为翻译单独使用一个JTextArea。我的程序弹出GUI,允许我输入一些内容,但是当我点击按钮时,没有任何反应。我查看了所有内容并且像是我已经将所有内容添加到JFrame中,但我无法弄清楚为什么什么都没有弹出。
// This program will take text that is English and translate it to Pig Latin


//import all the necessary packages for the program
import java.util.Scanner;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class PigLatin extends JFrame implements ActionListener{

public static final int WIDTH = 500;
public static final int HEIGHT= 400;
private JButton button;
private JTextArea original;
private JTextArea translation;
private JLabel english;
private JLabel pig;

public static void main(String[]args)
{
    PigLatin gui = new PigLatin();
    gui.setVisible(true);
    /*
    Scanner scan = new Scanner("far");
    Scanner scan1 = new Scanner("and");
    Scanner scan2 = new Scanner("away");

    while (scan.hasNext())
    {
        //prints out original words
    System.out.println(scan.next());
    System.out.println(scan1.next());
    System.out.println(scan2.next());
    } 

*/  
}

public PigLatin()
{   //titles the box, creates size and what to do when 'x' is clicked
    super("Pig latin translator");
    this.setSize(WIDTH,HEIGHT);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    this.setVisible(true);

    //creates frame for everything to be added to 
    JPanel frame = new JPanel();

    //creates labels for text boxes
    english = new JLabel("English");
    add(english);


    //enters in origninal text
    original = new JTextArea("Enter text here");
    original.setEditable(true);
    original.setLineWrap(true);

    //adds the text area to the GUI
    frame.add(original);


    //create text area for translation
    translation = new JTextArea();
    translation.setEditable(false);
    translation.setLineWrap(true);

    //adds the text area to the GUI
    frame.add(translation);

    //adds the frame to the GUI
    add(frame);
    frame.setVisible(true);

    //creates panel for the button
    JPanel buttons = new JPanel();
    buttons.setLayout(new FlowLayout());

    //create translate button
    button = new JButton("TRANSLATE TO PIG LATIN");
    button.addActionListener(this);

    buttons.add(button);

    JButton clear = new JButton("Clear");

    buttons.add(clear);

    //adds the buttons JPanel to the GUI
    add(buttons);
}
private int findWordEnd(String text) {

       int indexOfSpace = text.indexOf(" ");

       if (indexOfSpace == -1)  // Happens if there is no space
           return text.length();
       else
           return indexOfSpace;
    }

private int findVowel(String vowel)
{
    int i;
    //looks for vowel
    for(i=0; i<vowel.length();i++)
    {
        if(vowel.charAt(i)=='a'||vowel.charAt(i)=='e'||vowel.charAt(i)=='i'||vowel.charAt(i)=='o'||vowel.charAt(i)=='u')
            return i;
    }
    //if no vowels
    return vowel.length();
}

private String englishToPig(String text)
{
    String translate = "";

    while(!text.equals(""))
    {   
        //finds the first word
        int wordEndIndex = findWordEnd(text);
        String word = text.substring(0, wordEndIndex);

        //finds the start and end in Pig Latin
        int vowelIndex = findVowel(word);
        String start = word.substring(vowelIndex);
        String end = "-"+word.substring(0, vowelIndex)+"ay";

        //creates the translation
        translate = translate+start+end;
        //gets rid of first word, so translation can continue
        text = text.substring(wordEndIndex).trim();
    }
    return translate;
}
public void actionPerformed(ActionEvent e){
    if (e.getSource() == "TRANSLATE TO PIG LATIN") {

        // Get the English they entered
        String text = original.getText();
        text = text.trim();
        text = text.toLowerCase();

        // Display the translation
        translation.setText(englishToPig(text));
    }
}

}

1
e.getSource() == "翻译成 Pig Latin" ... getSource 是按钮。 - pL4Gu33
1
如果(e.getSource() == button) { - Madhawa Priyashantha
1个回答

2

点击事件的源头是按钮。你感兴趣的是动作命令。

if (e.getActionCommand().equals("TRANSLATE TO PIG LATIN")) {

还要注意使用equals()而不是==。然而,通常没有必要为所有事情使用相同的操作监听器,然后在监听器中确定源之间的区别。当源被保证是您感兴趣的源时,您也可以使用匿名类:
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // Get the English they entered
        String text = original.getText();
        text = text.trim();
        text = text.toLowerCase();

        // Display the translation
        translation.setText(englishToPig(text));
    }
});

清除按钮可以同样拥有自己的功能,因为它们的功能并不真正相关。


我之前没有意识到当我输入“get”时,它会选择“getSource”。我讨厌Eclipse的自动编译。谢谢你的帮助。 - Alicia
@Alicia 不用谢。getSource() 有其作用,只是这次没有正确使用。你可以将其与 button 进行比较,就可以正常工作了。个人而言,我会使用匿名类。 - kiheru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接