Ok the program runs, and it almost does exactly what I want it to do. I'm trying to accomplish an English to PigLatin translator i've got the program to translate from english to piglatin but for some reason i can only get it to translate onto the cmd screen and not the event driving part.
On the, i guess you could call it JFrame area or JText It will only print onto that part of the screen the last part of the word typed into the box. Once i hit the translate button this is when that happens. I need it to translate the whole entire English phrase to the piglatin phrase not just the last part of the English phrase.
here is the code
CODE
/**
* This program uses Swing to allow a user to input some English
* text into a JTextArea and click a button to convert the text
* to pig latin. The translated text is displayed in a separate
* JTextArea.
*/
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.String;
import java.util.*;
/**
* PigLatin conversion program.
*/
class Lab09 extends JFrame implements ActionListener
{
/**
* the constructor creates the window
* and the GUI widgets. The button click
* is the only control with an action listener.
*/
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
public static final int MAX_ROW = 25;
public static final int MAX_COL = 25;
public Lab09()
{
super("Pig Latin Translator");
this.setSize( WIDTH, HEIGHT );
//...done i think
//WIDTH = 300;
//HEIGHT = 200;
//hint:set default close operation and layout
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// source panel for input text
JPanel sourcePanel = new JPanel();
sourcePanel.setLayout( new GridLayout( 2, 1 ) );
sourcePanel.setBackground(Color.BLUE);
JLabel sourceLabel = new JLabel( "enter source text here:" );
sourcePanel.add( sourceLabel );
sourcePanel.setBackground(Color.BLUE);
sourceText = new JTextArea( MAX_ROW, MAX_COL );
sourcePanel.add( sourceText );
add( sourcePanel, BorderLayout.WEST );
// translated panel for Pig Latin text
JPanel translatedPanel = new JPanel();
translatedPanel.setLayout( new GridLayout( 2, 1 ) );
JLabel translatedLabel = new JLabel("translated to Pig Latin:");
translatedPanel.add( translatedLabel );
translatedPanel.setBackground(Color.PINK);
//JTextArea translatedText component manipulation
//...done
translatedText = new JTextArea(MAX_ROW, MAX_COL );
translatedPanel.add( translatedText );
add( translatedPanel, BorderLayout.EAST);
// button to ask for translation from source to Pig Latin
//... done
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.setBackground(Color.RED);
JButton translationButton = new JButton("translate");
translationButton.addActionListener(this);
buttonPanel.add(translationButton);
buttonText = new JTextArea( MAX_ROW, MAX_COL );
add(buttonPanel, BorderLayout.SOUTH);
}
/**
* when the button is clicked this event is invoked.
* This method uses a Scanner to parse each word,
* each word is converted to pig latin, and concatenated
* onto a string that is output in the translatedText area.
*/
public void actionPerformed( ActionEvent e )
{
String src = sourceText.getText();
String actionCommand = e.getActionCommand();
System.err.println("src = " + src);
StringBuffer result = new StringBuffer();
StringTokenizer lexer;
String vowels = "aeiou";
String vowelRule = "way";
String consonantRule = "ay";
String token;
String temp;
boolean x;
lexer = new StringTokenizer(src);
while (lexer.hasMoreTokens())
{
token = lexer.nextToken();
result.append(token);
if (vowels.indexOf(token.substring(0,1)) > -1 )
{
x=true;
}
else
{
x=false;
}
if( x == true )
{
result.append(token);
translatedText.setText(token +vowelRule +" ");
translatedText.JTextArea(token +vowelRule +" ");
System.out.print(token +vowelRule +" ");
}
else if ( x == false )
{
temp = token;
temp = token.substring(1);
token = token.substring(0,1);
System.out.print(temp +token +consonantRule +" ");
translatedText.setText(temp +token +consonantRule +" ");
}
}
//...not even close
//if(actionCommand.equals("translate"))
//{
//}
//...not even close
//if(sourceText.equals("translate"))
//{
// //.setText("Hello");
//}
//...not even close
// scan from a string instead of the console
//Scanner scan = new Scanner( src );
//translatedText.setText(src);
// iterates through each word
//...
// display translation in the JTextArea translatedText
//...
}
/**
* the input word to convert to pig latin.
* If the first letter is a vowel then add "way" to the end.
* Otherwise move the first letter to the end of the word and add "ay".
*/
public String convert( String word )
{
//...
return "";
}
// main method just displays the window
public static void main( String[] args )
{
Lab09 piggy = new Lab09();
piggy.setVisible( true );
}
// Declaration of missing attributes corresponding to text area
// dimensions
private JTextArea buttonText;
// where user enters English text
private JTextArea sourceText;
// where program displays translated text
private JTextArea translatedText;
}
This post has been edited by Suspect1: 19 Mar, 2007 - 03:01 AM