Swing GUI和转换器问题



所以我有一个类项目,我必须创建一个翻译,用户可以输入文本,在英语中,点击一个按钮,并获得猪拉丁文翻译。我必须使用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));
    }
}
}

click事件的来源是按钮。您感兴趣的是操作命令。

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

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

相关内容

  • 没有找到相关文章

最新更新