3 Replies - 630 Views - Last Post: 25 October 2009 - 01:43 PM Rate Topic: -----

#1 zanyrogue  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Need help putting info into my Jpanel

Post icon  Posted 25 October 2009 - 11:42 AM

Hi,

I need help getting my int guess to appear in my Jpanel so that the user can see the first random guess and their guesses from then on until the guess is correct.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class C06GuessingGame extends JFrame implements ActionListener {
	public static final long serialVersionUID = 1L;

	JFrame window = null;
	
	JTextField Box = new JTextField("Box");
	JButton High = new JButton("High");
	JButton Low = new JButton("Low");
	JButton Correct = new JButton("Correct");

	// Variables for random and guess numbers
	int Highest = 1000;
	int Lowest = 0;
	int Counter = 0;
	Random r = new Random();
	int random = r.nextInt(1000) + 1;

	public C06GuessingGame() {
		
		window = new JFrame();
		setSize(250, 150);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Guessing Game");
		JPanel pane = new JPanel();
		pane.add(High);
		pane.add(Low);
		pane.add(Correct);
		add(pane);
		High.addActionListener(this);
		Low.addActionListener(this);
		Correct.addActionListener(this);

		setVisible(true);
	}

	public static void main(String[] args) {

		new C06GuessingGame();
	}

	public void actionPerformed(ActionEvent e) {
		
		int guess = random;<--HOW CAN I MAKE THIS APPEAR IN MY JPANEL UNDERNEATH HIGH, LOW AND CORRECT?
		
//System.out.println(guess);

		if (e.getSource() == Low) {
			Lowest = guess;
			random = (Highest + Lowest) / 2;
			Counter++;

		} else if (e.getSource() == High) {
			Highest = guess;
			random = (Highest + Lowest) / 2;
			Counter++;

		} else {
			JOptionPane.showMessageDialog(window,("CORRECT!!! The number of guesses taken were: " + Counter ++));
			window.setVisible(false);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help putting info into my Jpanel

#2 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Need help putting info into my Jpanel

Posted 25 October 2009 - 12:03 PM

you can add a JLabel g and each time user guesses a value you can do:g.setText(guess+"");
Was This Post Helpful? 0
  • +
  • -

#3 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: Need help putting info into my Jpanel

Posted 25 October 2009 - 12:07 PM

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.JLabel;
import java.awt.BorderLayout;

public class C06GuessingGame extends JFrame implements ActionListener {
	public static final long serialVersionUID = 1L;

	JFrame window = null;
   
	JTextField Box = new JTextField("Box");
	JButton High = new JButton("High");
	JButton Low = new JButton("Low");
	JButton Correct = new JButton("Correct");
	
	JLabel lblGuess;
	JPanel pGuess;

	// Variables for random and guess numbers
	int Highest = 1000;
	int Lowest = 0;
	int Counter = 0;
	Random r = new Random();
	int random = r.nextInt(1000) + 1;

	public C06GuessingGame() {
	   
		setSize(250, 150);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Guessing Game");
		JPanel pane = new JPanel();
		pane.add(High);
		pane.add(Low);
		pane.add(Correct);
		add(pane, BorderLayout.NORTH);
		
		lblGuess = new JLabel("", JLabel.CENTER);
		pGuess = new JPanel();
		pGuess.add(lblGuess);
		add(pGuess, BorderLayout.CENTER);
			
		High.addActionListener(this);
		Low.addActionListener(this);
		Correct.addActionListener(this);

		setVisible(true);
	}

	public static void main(String[] args) {

		new C06GuessingGame();
	}

	public void actionPerformed(ActionEvent e) {
	   
		int guess = random; //<--HOW CAN I MAKE THIS APPEAR IN MY JPANEL UNDERNEATH HIGH, LOW AND CORRECT?
		lblGuess.setText(""+guess);
//System.out.println(guess);

		if (e.getSource() == Low) {
			Lowest = guess;
			random = (Highest + Lowest) / 2;
			Counter++;

		} else if (e.getSource() == High) {
			Highest = guess;
			random = (Highest + Lowest) / 2;
			Counter++;

		} else {
			JOptionPane.showMessageDialog(window,("CORRECT!!! The number of guesses taken were: " + Counter));
		}
	}
}

Was This Post Helpful? 1
  • +
  • -

#4 zanyrogue  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Re: Need help putting info into my Jpanel

Posted 25 October 2009 - 01:43 PM

Brilliant, thank you both your ideas worked a treat. so easy and I just couldn't see it! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1