Greetings,
I am trying to write a simple MadLib program and I am having trouble when it comes to displaying the data at the end of the program. It appears that it all crams together and the text is near unreadable. Am I doing something incorrectly?
Thanks in advance.
I am trying to write a simple MadLib program and I am having trouble when it comes to displaying the data at the end of the program. It appears that it all crams together and the text is near unreadable. Am I doing something incorrectly?
Thanks in advance.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JMadLib extends JApplet implements ActionListener
{
//create container, label and button
Container con = getContentPane();
JLabel header = new JLabel("Welcome to Mad Libs");
JLabel message = new JLabel("Enter a noun");
JLabel story = new JLabel("");
JTextField prompts = new JTextField(5);
JButton pressMe = new JButton("Press Me");
public int i = 0;
public void init()
{
//fill container with labels, text fields, and a button
con.add(header);
con.add(message);
con.add(prompts);
con.add(pressMe);
con.setLayout(new FlowLayout());
//add event handler to button
pressMe.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
final int ROWS = 4;
final int COLUMNS = 2;
String[][] words = {
{"", ""},
{"Enter another noun", ""},
{"Enter an adjective", ""},
{"Enter a past tense verb", ""}
};
if(source == pressMe && i < 3)
{
words[i][COLUMNS - 1] = prompts.getText();
message.setText(words[i + 1][0]);
prompts.setText("");
i++;
}
else if(source == pressMe && i == 3)
{
words[i][COLUMNS - 1] = prompts.getText();
remove(header);
remove(message);
remove(pressMe);
remove(prompts);
story.setText("Mary had a little " + words[0][1] +
"\n It's " + words[1][1] + " was " + words[3][1] +
" as snow \nAnd everywhere that Mary " + words[2][1] +
"\n The " + words[0][1] + " sure to go.");
con.add(story);
}
validate();
}
}