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.");
		prompt2 = new JLabel("Can you guess my number? Enter your Guess:");
		guessInput = new JTextField(5);
		guessInput.addActionListener(new GuessHandler());
		message = new JLabel("Guess result appears here.");

		// button starts a new game
		newGame = new JButton("New Game");
		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");
				background = Color.white;

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

	// change background color
	public void paint(Graphics g) {

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

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

			lastDistance = Math.abs(guess - number);
			if (guess > number)
				message.setText("Too High. Try a lower number.");
				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.

	} // end method react

	public static void main(String args[]) {
		JFrame myFrame = new JFrame("Guess My Number!!");
		JPanel Guess = new JPanel();
	    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());


I really appreciate it.

Recommended Answers

All 6 Replies

What exactly is the problem?

What exactly is the problem?

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

Without looking through all your code, have you set the frame visible on the panel?

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();

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.