Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Need Help with Random Number Generator


  • Please log in to reply

#1
Scyrus

Scyrus

    New Member

  • Member
  • Pip
  • 8 posts
I need to compile a frame that has a button (Generate) that generates a random number to the side of it from 1 thru 100 .('Random number is: __")

Here's what I've got so far, but I don't know what I need or don't need in this program.

import javax.swing.*;

		public class Activity4B {
	   public static void main( String[] args ) {
		 JFrame frame = new JFrame();
		 frame.setTitle( "Seconds Generator" );
		 frame.setBounds( 300, 300, 300, 200 );
		 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		  
		 frame.getContentPane().add( new ButtonPane1());
		 frame.setVisible (true);
		  
	  }
   }

and

class ButtonPane1MOD extends JPanel 
{
   
	  private JButton gen;
	  private JLabel resultLabel;
	  private int num = 1;
   	
	   public class ButtonPane1MOD()
		 {
	  
			Random generator = new Random();
			int r = x.nextInt(100) + 1
						random = new JButton("Generate");
		 
		  
		 gen.addActionListener(new ButtonsListener1());
	  
   	
	   private class ButtonsListener1 implements ActionListener {
	  
		  public void actionPerformed (ActionEvent evt)
		 {
			num++;
			resultLabel.setText("Random number is: " + num1);
		 }

			num--;
			resultLabel.setText(" " + r);
		 }
	  
}

Edited by Scyrus, 15 February 2006 - 10:06 PM.

  • 0

Advertisements


#2
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Your ButtonPanel class should be something like this:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

import java.util.Random;

class ButtonPanel extends JPanel {
	private JButton randomButton = new JButton("Generate");
	private JLabel resultLabel = new JLabel();
	   
	public class ButtonPanel() {
		Random generator = new Random();

		randomButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resultLabel.setText(String.valueOf(generator.nextInt(100)));
			}
		});
	}
}
That code shouldn't work. It is just meant to be a pointer in the right direction. You'll see that I used an anonymous class instead of an inner class. It doesn't really matter what you use; if you are more comfortable with inner classes, then by all means, use them.
  • 0

#3
Scyrus

Scyrus

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
I'm still getting an error in the line:

import javax.swing.*;

		class ButtonPanel 
		{
	   public static void main( String[] args ) {
		 JFrame frame = new JFrame();
		 frame.setTitle( "Seconds Generator" );
		 frame.setBounds( 300, 300, 300, 200 );
		 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
   ----->frame.getContentPane().add( new ButtonPanel()); <---------
		 frame.setVisible (true);
		  
	  }
   }

Activity4B.java:10: cannot resolve symbol
symbol  : method add (ButtonPane1)
location: class java.awt.Container
		 frame.getContentPane().add( new ButtonPane1());



Solution?

Edited by Scyrus, 16 February 2006 - 10:40 AM.

  • 0

#4
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
ButtonPanel must be a subclass of JPanel.

Edited by destin, 16 February 2006 - 03:55 PM.

  • 0

#5
Scyrus

Scyrus

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
So how do I go about setting it as a subclass to JPanel? Sorry, I'm really new at this . . .
  • 0

#6
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
class ButtonPanel extends JPanel {

	// ...

}

  • 0

#7
Scyrus

Scyrus

    New Member

  • Topic Starter
  • Member
  • Pip
  • 8 posts
Ok, new problem. I'm trying to compile another dialog box that converts seconds into hours, minutes, and seconds. I can't get this code right no matter how many times I try. Please, someone fix this:

class SecondsPane1 extends JPanel {

private JButton conv;
private JLabel inputLabel, resultLabel;
private JTextField secs;

public SecondsPane1(){

inputLabel = new JLabel("Enter seconds: ");
resultLabel = new JLabel("---");
conv = new JButton("Convert: ");
seconds = new JTextField (5);
conv.addActionListener (new ButtonListener());

add(inputLabel);
add(seconds);
add(conv);
add(resultLabel);
setBackground(Color.yellow);
}
private class ButtonListener implements ActionListener
{

public void actionPerformed (ActionEvent evt)
{
int hours, minutes, seconds, newseconds;

String text = secs.getText();

seconds = Integer.parseInt (text);
hours = seconds*3600;
minutes = (seconds%3600)/60;
newseconds = ((seconds%3600)%60);

resultLabel.setText (+seconds+" seconds(s) = " +hours+ " hour(s), " +minutes+ " minute(s), and " +newseconds+" second(s)");
}
} 
}


-and the JPanel-


import javax.swing.*;

		public class Project4ConvertingSeconds {
	   public static void main( String[] args ) {
		 JFrame frame = new JFrame();
		 frame.setTitle( "Converting Seconds" );
		 frame.setBounds( 300, 500, 300, 300 );
		 frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		  
		 frame.getContentPane().add( new SecondsPane1());
		 frame.setVisible (true);
		  
	  }
   }

  • 0

#8
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
It's always good to have compilable code before asking for help... try to fix your errors. If you need help with that, then post the errors you're having trouble fixing.

[edit]
Here's working code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HelpFile {
	public static void main( String[] args ) {
		JFrame frame = new JFrame("Seconds Converter");
		frame.getContentPane().add(new SecondsPanel());
		frame.setBounds(300, 500, 300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

class SecondsPanel extends JPanel {
	private JButton convertButton = new JButton("Convert");
	private JLabel inputLabel = new JLabel("Enter seconds:");
	private JLabel resultLabel = new JLabel();
	private JTextField secondField = new JTextField(5);
	
	public SecondsPanel() {
		convertButton.addActionListener(new ButtonListener());
		
		add(inputLabel);
		add(secondField);
		add(convertButton);
		add(resultLabel);
		setBackground(Color.yellow);
	}
	
	private class ButtonListener implements ActionListener {
		public void actionPerformed (ActionEvent e) {
			long seconds = Integer.parseInt(secondField.getText());
			int hours = seconds / 3600;
			int minutes = seconds % 3600 / 60;
			int newseconds = seconds % 3600 % 60;
			
			resultLabel.setText(hours + " hour(s), " + 
								minutes + " minute(s), and " + 
								newseconds + " second(s)");
		}
	} 
}
Make sure you see the differences between the two. Don't just copy and paste and say you're done. Not only did you have syntactical errors, but you had logic errors as well. Make sure you find them or this is pointless.

PS. Make a new thread next time; we're getting off topic.

Good luck!

Edited by destin, 19 February 2006 - 11:07 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP