Hello I'm having trouble extending JPanel. I know how to extend JFrame but my assignment calls for JPanel. Here is what I have:

import java.awt.Color;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Guess extends JPanel {

	private int number, guessCount;
	private int lastDistance;
	private JTextField guessInput;
	private JLabel prompt1, prompt2, message;
	private JButton newGame;
	private Color background;


	// set up GUI and initialize values
	public Guess() {
	
		
		setLayout(new FlowLayout());
		guessCount = 0;
		background = Color.white;

		// create GUI components
		prompt1 = new JLabel("I have a number between 1 and 1000.");
		this.add(prompt1);
		prompt2 = new JLabel("Can you guess my number? Enter your Guess:");
		add(prompt2);
		guessInput = new JTextField(5);
		guessInput.addActionListener(new GuessHandler());
		add(guessInput);
		message = new JLabel("Guess result appears here.");
		add(message);

		// button starts a new game
		newGame = new JButton("New Game");
		add(newGame);
		newGame.addActionListener(new ActionListener()
		
		{
			public void actionPerformed(ActionEvent e) {
				/*
				 * A JButton should be provided to allow the user to play the
				 * game again. When the JButton is clicked, a new random number
				 * should be generated and the input JTextField changed to be
				 * editable.
				 */
				message.setText("Guess Result");
				guessInput.setText("");
				guessInput.setEditable(true);
				background = Color.white;
				theGame();
				repaint();
			}
		}
            );
		theGame();
	}

	// choose a new random number
	public void theGame() {
		number = (int) (Math.random() * 1000 + 1);
		guessCount = 0;
	}

	// change background color
	public void paint(Graphics g) {
		super.paint(g);
		setBackground(background);
	}

	// react to new guess
	public void react(int guess) {
		guessCount++;
		int currentDistance = 1000;

		// first guess
		if (guessCount == 1) {

			lastDistance = Math.abs(guess - number);
			if (guess > number)
				message.setText("Too High. Try a lower number.");
			else
				message.setText("Too Low. Try a higher number.");

		} else {

			currentDistance = Math.abs(guess - number);

			// guess is too high
			if (guess > number) {

				message.setText("Too High. Try a lower number.");
				background = (currentDistance <= lastDistance) ? Color.red
						: Color.blue;
				lastDistance = currentDistance;

			}
			// guess is too low
			else if (guess < number) {

				message.setText("Too Low. Try a higher number.");
				background = (currentDistance <= lastDistance) ? Color.red
						: Color.blue;
				lastDistance = currentDistance;

			}
			// guess is correct
			else {
				/*
				 * When the user gets the correct answer, “Correct!” should be
				 * displayed, and the JTextField used for input should be
				 * changed to be uneditable.
				 */
				message.setText("Correct!");
				guessInput.setEditable(false);
				setBackground(Color.white);
			}

			repaint();
		}
	} // end method react

	public static void main(String args[]) {
		JFrame myFrame = new JFrame("Guess My Number!!");
		JPanel Guess = new JPanel();
		myFrame.add(Guess);
		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    myFrame.setSize( 400, 400 ); // set frame size
	    myFrame.setVisible( true ); // display frame

	}

	// inner class acts on user input
	class GuessHandler implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			int guess = Integer.parseInt(guessInput.getText());
			react(guess);
			guessInput.selectAll();
		}

	}
}

I really appreciate it.

What exactly is the problem?

When the program runs, the JFrame comes up but the contents of the JPanel do not come up...

yeah i believe so, i have myFrame.setVisible(true) in the main program

Look at your declaration of the panel

JPanel Guess = new JPanel();

You have declared a plain JPanel here named "Guess" and added it to your frame. You actually need to declare an instance of your "Guess" class and add that to the frame

Guess guessPanel = new Guess();
myFrame.add(guessPanel);

That worked. Thank you so much. I knew it was right in front of me, but until someone else looked at it, I wasn't going to get it. Thank you again!

This question has already been answered. Start a new discussion instead.