Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with JLabel text overlapping

Status
Not open for further replies.

utahjzz98

Programmer
Jan 9, 2003
69
US
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.

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();
      }
   }
 
Why is story an attribute, and not a local variable?
Why is i public?
However, to show multiple rows you either need html-formating:
Code:
JLabel story = new JLabel ("<html>Mary had a little " + words[0][1] + "<br />It's " + words[1][1] + " was " + words[3][1] + " as snow <br />And everywhere that Mary " + words[2][1]
+ "< br/>The " + words[0][1] + " sure to go.");
or use a JTextArea, and make it uneditable yourself:
Code:
JTextArea story = new JTextArea ("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.");
story.setEditable (false);

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top