Pig Latin Gui question

I have to create a GUI program for the Pig Latin class

Page 1 of 1

3 Replies - 3904 Views - Last Post: 03 February 2008 - 12:33 PM Rate Topic: -----

#1 GIGI03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-February 08

Pig Latin Gui question

Posted 03 February 2008 - 08:39 AM

import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

	public class PigLatinPanel extends JPanel 
   {
	  private JTextField sentence;
	  private JLabel label;
   
	   public PigLatinPanel ()
	  {
		 sentence = new JTextField ();
		 sentence.addActionListener (new JTextFieldListener());	  
		 label = new JLabel ("Please enter a sentence");
	  
		 add (sentence);
		 add (label);
	  
		 setPreferredSize (new Dimension(300,75));
		 setBackground (Color.yellow);
	  }
   
	   private class JTextFieldListener implements ActionListener
	  {
		  public void actionPerformed (ActionEvent event)
		 {	
			String text = sentence.getText();
			System.out.println("Sentence:" + text);
		 
			
		 }
	  }
   
   
   }


import java.util.Scanner;

	public class PigLatinTranslator
   {
   //-----------------------------------------------------------------
   //  Translates a sentence of words into Pig Latin.
   //-----------------------------------------------------------------
	   public static String translate (String sentence)
	  {
		 String result = "";
	  
		 Scanner scan = new Scanner (sentence);
	  
		 sentence = sentence.toLowerCase();
	  
		 while (scan.hasNext())
		 {
			result += translateWord (scan.next());
			result += " ";
		 }
	  
		 return result;
	  }
 

   //-----------------------------------------------------------------
   //  Translates one word into Pig Latin. If the word begins with a
   //  vowel, the suffix "yay" is appended to the word.  Otherwise,
   //  the first letter or two are moved to the end of the word,
   //  and "ay" is appended.
   //-----------------------------------------------------------------
	   private static String translateWord (String word)
	  {
		 String result = "";
	  
		 if (beginsWithVowel(word))
			result = word + "yay";
		 else
			if (beginsWithBlend(word))
			   result = word.substring(2) + word.substring(0,2) + "ay";
			else
			   result = word.substring(1) + word.charAt(0) + "ay";
	  
		 return result;
	  }
   
   //-----------------------------------------------------------------
   //  Determines if the specified word begins with a vowel.
   //-----------------------------------------------------------------
	   private static boolean beginsWithVowel (String word)
	  {
		 String vowels = "aeiou";
	  
		 char letter = word.charAt(0);
	  
		 return (vowels.indexOf(letter) != -1);
	  }
	  
   //-----------------------------------------------------------------
   //  Determines if the specified word begins with a particular
   //  two-character consonant blend.
   //-----------------------------------------------------------------
	   private static boolean beginsWithBlend (String word)
	  {
		 return ( word.startsWith ("bl") || word.startsWith ("sc") ||
			   word.startsWith ("br") || word.startsWith ("sh") ||
			   word.startsWith ("ch") || word.startsWith ("sk") ||
			   word.startsWith ("cl") || word.startsWith ("sl") ||
			   word.startsWith ("cr") || word.startsWith ("sn") ||
			   word.startsWith ("dr") || word.startsWith ("sm") ||
			   word.startsWith ("dw") || word.startsWith ("sp") ||
			   word.startsWith ("fl") || word.startsWith ("sq") ||
			   word.startsWith ("fr") || word.startsWith ("st") ||
			   word.startsWith ("gl") || word.startsWith ("sw") ||
			   word.startsWith ("gr") || word.startsWith ("th") ||
			   word.startsWith ("kl") || word.startsWith ("tr") ||
			   word.startsWith ("ph") || word.startsWith ("tw") ||
			   word.startsWith ("pl") || word.startsWith ("wh") ||
			   word.startsWith ("pr") || word.startsWith ("wr") ); 
	  }
   }


import javax.swing.JFrame;

	public class PigLatinDisplay 
   
   {
	   public static void main (String[] args)
	  {
		 JFrame frame = new JFrame ("Pig Latin");
		 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	  
		 PigLatinPanel panel = new PigLatinPanel();
		 frame.getContentPane().add(panel);
	  
		 frame.pack();
		 frame.setVisible(true);
	  }
   }


I am suppose to have a textfield box to input a word but I dont understand what I am doing wrong, it is not being displayed.The listener code should take the string from the text box and then
call the method in the PigLatinTranslator class to do the translation, but I don't know how to call it. I am having difficulties trying to do this. Any help will be appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin Gui question

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Pig Latin Gui question

Posted 03 February 2008 - 09:52 AM

Well, your first problem is that you haven't declared or initialized any textfield objects.
Was This Post Helpful? 0
  • +
  • -

#3 GIGI03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-February 08

Re: Pig Latin Gui question

Posted 03 February 2008 - 09:57 AM

View PostGWatt, on 3 Feb, 2008 - 09:52 AM, said:

Well, your first problem is that you haven't declared or initialized any textfield objects.


Oh I though I did were I have new JTextField();
I fixed the problem.

The listener code should take the string from the text box and then call the method in the PigLatinTranslator class to do the translation, but how do I call it? Thanks for the help! :)

This post has been edited by GIGI03: 03 February 2008 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Pig Latin Gui question

Posted 03 February 2008 - 12:33 PM

I suspect one would call the .translate() method of the PigLatinTranslator class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1