I am still confused as to how i can call the method from the Translator and run it through the button. Here is the translator code and the code for the frame with the main method. Any advice will greatly enhance my learning experience sense i have spent hours reading the Java documentations. BTW, this is only my second homework assignment!
// Here is the Translator
import java.util.Scanner;
public class Translator
{
//public static void main(String[] args)
public String translate()
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine())
{
String line = sc.nextLine().trim().toLowerCase();
while(line.length() > 0) {
int blank = line.indexOf(' ');
String word = "";
if(blank > 0) {
word = line.substring(0, blank);
line = line.substring(blank).trim();
} else {
word = line;
line = "";
}
int a = word.indexOf('a');
int e = word.indexOf('e');
int i = word.indexOf('i');
int o = word.indexOf('o');
int u = word.indexOf('u');
if((a == -1)&&(e == -1) && (i == -1) &&
(o == -1) && (u == -1))
{
word = word + "ay";
} else {
if((a == 0)||(e == 0)||(i == 0) || (o == 0) ||
(u == 0))
{
word = word + "way";
} else {
int n = word.length();
if((a >= 0) && (a < n))
n = a;
if((e >= 0) && (e < n))
n = e;
if((i >= 0) && (i < n))
n = i;
if((o >= 0) && (o < n))
n = o;
if((u >= 0) && (u < n))
n = u;
word = word.substring(n) +
word.substring(0, n) + "ay";
}
}
System.out.print(word + " ");
}
//System.out.println();
}
}
}
// Here is the frame class with the button and main
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
/**
This is a frame class for the translator
9.24.09
*/
public class jchan extends JFrame{
private static JFrame window;
private static JTextArea jt1;
private static JTextArea jt2;
private static JScrollPane areaScrollPaneE;
private static JScrollPane areaScrollPaneW;
public jchan(){
}
public static void createAndShowGUI(){
window = new jchan();
window.setTitle("Pig Latin Translator");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
window.setLayout(new FlowLayout());
window.setLayout(new BorderLayout());
// Creates the JTextAreas
jt1 = new JTextArea("Enter text here");
jt2 = new JTextArea();
window.add(jt1);
window.add(jt2);
// Allows for scrolling of the text area west
JScrollPane areaScrollPaneW = new JScrollPane(jt1);
areaScrollPaneW.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPaneW.setPreferredSize(new Dimension(300,100));
window.add(areaScrollPaneW, BorderLayout.WEST);
// Allows for scrolling of the text area east
JScrollPane areaScrollPaneE = new JScrollPane(jt2);
areaScrollPaneE.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPaneE.setPreferredSize(new Dimension(300,100));
window.add(areaScrollPaneE, BorderLayout.EAST);
// Button Panel South
JPanel buttonPanel = new JPanel( );
buttonPanel.setBackground(Color.BLACK);
buttonPanel.setLayout(new FlowLayout( ));
JButton button = new JButton("Translate");
buttonPanel.add(button);
window.add(buttonPanel, BorderLayout.SOUTH);
button.addActionListener(new ActionListener(){
// THIS IS WHERE IAM HAVING THE PROBLEM CALLING THE METHOD
@Override
public void actionPerformed(ActionEvent arg0) {
jt2.setText(translate(jt1.getText()));
}
});
//window.add(button);
window.pack();
window.setSize(700, 250);
window.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Thanks.....