Results 1 to 3 of 3
  1. #1
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Using JLayeredPanes to Make a Background

    Okay, so I'm experimenting with trying to use a JLayeredPane to create a background for my applet. I thought it would be simple in principle, components assigned higher integers when added to the pane are shown above components that are assigned lower integers. Seems simple enough. The problem is, when I tried to use that method in my code, my layout got messed up, and no component seemed to want to be displayed on top of other components. I wish I had some idea as to why, but honestly I'm drawing a blank. I have my GUI code below. Would anyone be able to give it a quick glance over to see what I'm doing wrong? I would be very grateful for any assistance.

    Java Code:
    public class MainProgram extends JApplet implements ActionListener
    {
    //Declare all the necessary variables and initialize as needed.
    private TextField userInput;
    private JTextArea output;
    private JScrollPane scrollPane;
    private boolean canProceed;
    static final long serialVersionUID = 1; // added in an attempt to fix an error, highly confused as to what it does.
    public String story = "My Little Pony: Friendship is Magic is the property of\n" +
    		"Hasbro, Lauren Faust, and the rest of its creative team. \n" +
    		"Please support the official release. \n\n" +
    		"Note: Your choices matter, and the events of this\n" +
    		"game occur before Princess Celestia's age reached double digits. \n\n" +
    		"Your eyes are bleary... you feel drained, so very " +
    		"drained... and dizzy. \n" +
    		"You tilt your head over to " +
    		"the side of the rickety cot you're lying on and \n" +
    		"manage to throw up. To your surprise, there's a " +
    		"basin waiting for you.\n" +
    		"\"Hey there,\" comes a voice.\n" +
    		"You see an Earth Pony mare in an orange vest sitting at your bedside.\n" +
    		"She gives you a half hearted smile. \"Welcome to the land of the living.\" \n" +
    		"\"Can you tell me your name?\" She asks. \n" +
    		"1)Heavy Hooves\n" +
    		"2)Careful Crafts\n" +
    		"3)Free Faller\n" +
    		"4)Lie about name\n\n";
    public String input;
    StoryTeller author = new StoryTeller(0, 0, 0, 0, 0, 0);
    Story theStory = new Story();
    
    public void init()
    {
    	
    	//Add the necessary content pane and set the layout.
    	JLayeredPane contentPane = new JLayeredPane();
    	contentPane.setLayout(new FlowLayout());
    	contentPane.setBackground(Color.DARK_GRAY);
    	
    	//Set the content pane to the layered pane
    	this.setContentPane(contentPane);
    	
    	
    	//Create the TextFeild for user input.
    	userInput = new TextField("", 15);
    	userInput.setText("Enter your choice here");
    	userInput.addActionListener(this);
    	contentPane.add(userInput, 1);
    	
    
    	
    	//Create the JTextArea for the display of story.
    	output = new JTextArea(40, 115);
    	scrollPane = new JScrollPane(output);
    	output.setFont(new Font("Courier", Font.PLAIN, 12));
    	output.setText(story);
    	contentPane.add(scrollPane, 1);
    	
    	//Create Wounded Label
    	JLabel title = new JLabel();
    	title.setText("WOUNDED");
    	title.setFont(new Font("Courier", Font.BOLD, 48));
    	title.setForeground(Color.BLACK);
    	contentPane.add(title, 1);
    	
    	//Create the icon for the background label
    	try{
    	JLabel background =  new JLabel(new ImageIcon(ImageIO.read(new File("background.jpg"))));
    	contentPane.add(background, 0);
    	}
    	catch (Exception e)
    	{
    		System.out.println("Error, file not found");
    	}
    	
    	
    	
    	/*
    	//Create the essential choose button to signal that
    	//the program should retrieve user input from userInput.
    	JButton chooseButton = new JButton();
    	chooseButton.setText("Choose");
    	chooseButton.addActionListener(this);
    	contentPane.add(chooseButton);
    	*/
    }
    
    /*Method that realizes when chooseButton has been pressed 
     * and then stores the string entered into userInput
     * in input. Then it checks if it can proceed with that
     * input by calling the author.readInput method which
     * also returns a boolean which is stored in canProceed.
     * If canProceed is true, the program invokes the 
     * author.continueStory method which returns the new story 
     * (among other things discussed in the documentation 
     * for StoryTeller). If canProceed is false, then 
     * an error message is added to story. In either case
     * the text of output is set to story.
    */
    public void actionPerformed(ActionEvent e)
    {
    	/*String	actionCommand = e.getActionCommand();
    	if (actionCommand.equals("Choose"))
    	{
    	*/
    		input = userInput.getText();
    		canProceed = author.readInput(input);
    		if (canProceed)
    		{
    		story = author.continueStory(story, theStory);
    		output.setText(story);
    		}
    		else
    		{
    			story = story + "\n Not a valid choice, choose again:\n";
    			output.setText(story);
    		}
    		userInput.setText("");
    	/*
    	}
    	*/
    }
    
    public static void main(String[] args)
    {
    	
    }
    
    }
    Last edited by chaucer345; 07-01-2013 at 12:01 AM.

  2. #2
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Using JLayeredPanes to Make a Background

    I figured it out.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: Using JLayeredPanes to Make a Background

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 09-01-2012, 01:22 PM
  2. How to make Java program run in the background?
    By divs1210 in forum Advanced Java
    Replies: 7
    Last Post: 04-16-2011, 05:30 PM
  3. How to make a background image
    By Purple Turtle in forum Java Software
    Replies: 2
    Last Post: 11-04-2010, 07:32 AM
  4. How do I make a timer run in the background?
    By caryr in forum Advanced Java
    Replies: 15
    Last Post: 09-04-2009, 02:34 AM
  5. How to make the JTextArea background invisible
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •