【问题标题】:Swing GUI and translator issuesSwing GUI 和翻译器问题
【发布时间】:2015-07-18 14:15:21
【问题描述】:

所以我有一个班级项目,我必须在其中创建一个翻译器,用户可以在其中输入文本、英文、点击按钮并获得 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));
    }
}

}

【问题讨论】:

  • e.getSource() == "TRANSLATE TO PIG LATIN" ... getSource 是按钮。
  • if (e.getSource() == button) {??

标签: java swing user-interface flowlayout


【解决方案1】:

点击事件的来源是按钮。你感兴趣的是action命令。

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 不客气。 getSource() 是有目的的,只是这次没有正确使用。您可以将它与button 进行比较,它会起作用的。我个人还是会使用匿名类。
猜你喜欢
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 2012-10-27
相关资源
最近更新 更多