Jump to content

Problem with array in JApplet

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Qistina Tajuddin

Qistina Tajuddin

    Newbie

  • Members
  • Pip
  • 2 posts
This is an applet to help people learn what recursion is. I'm trying to use stepCount to go through the comment array as the user clicks the previous and next buttons. However, stepCount just stays stuck at 0. Please help out, thanks!

import java.awt.*;

import java.util.*;

import java.awt.event.*;

import javax.swing.*;


public class NewJApplet extends JApplet {


	private JPanel mainPanel = new JPanel();

	static int stepCount = 0;

	

	// Comments

	

	private JButton nextButton = new JButton ("Next");

	private JButton previousButton = new JButton ("Previous");

	private JButton restartButton = new JButton ("Restart");

	

	JLabel[] comments = {

		new JLabel ("Comment 0"), 

		new JLabel ("Comment 1"), 

		new JLabel ("Comment 2"), 

		new JLabel ("Comment 3"), 

		new JLabel ("Comment 4"), 

		new JLabel ("Comment 5") 

	};


	public void init () {

	

		createPanels ();

	

	}


	private void createPanels () {

	

		CommentPanel commentPanel = new CommentPanel (this);


		mainPanel.add (commentPanel);

		getContentPane().add (mainPanel);

	

	}

	

	class CommentPanel extends JPanel {


		public CommentPanel (final NewJApplet newapplet) {

		

			add (comments[stepCount]);

			add (nextButton);

			add (previousButton);

			add (restartButton);

			

			nextButton.addActionListener (new buttonListener());

			previousButton.addActionListener (new buttonListener());

			restartButton.addActionListener (new buttonListener());

			

			setBorder(BorderFactory.createTitledBorder("Comments" + "(Current Step Count: " + stepCount + ")"));

			

		}

	   

	}


	

	class buttonListener implements ActionListener {

						

		public void actionPerformed (ActionEvent event) {


			if (event.getSource () == nextButton) {

				stepCount++;

			}

				

			else if (event.getSource () == previousButton)

				stepCount--;

				

			else

				stepCount = 0;

				

		}


	}

	 

}



Edited by Qistina Tajuddin, 21 August 2011 - 02:26 AM.
Updated the code to make it more legible.


#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
event.getSource () == nextButton

try using

event.getSource ().equals(nextButton)
instead




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users