Results 1 to 3 of 3
  1. #1
    flameofSuzaku is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default Non functional radio buttons and messed up array

    Hi. I've been working on a homework problem and ran into a bunch of little things that went wrong. The assignment is to make a magic 8Ball and for extra point to add radio buttons to toggle btwn the 8Ball and a Fortune teller.

    We're supposed to randomly display the responses which are stored as an array. I can only get it to select one random number. Even if i click the button alot, its the same response. The radio buttons are visible but the event I put for them don't work.

    Can someone take a look and tell me what i did wrong? I tried rearranging things to get the radio buttons to work that that didn't help.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Random;
    
    public class EightBallWindow extends JFrame {
    	
    	private JPanel panel1;
    	private JPanel panel2;
    	private JPanel panel3;
    	private JPanel panel4;
    	private JRadioButton eightBallradio;
    	private JRadioButton fortuneradio;
    	private JLabel question;
    	private JTextArea textArea;
    	private JButton predictButton;
    	private JButton fortuneButton;
    	private JButton clearButton;
    	int rows = 20;
    	int cols = 30;
    	
    	//Constructs the eightball window
    	public EightBallWindow()
    	{
    		setTitle("The Magic 8 Ball");
    		
    		setSize(800, 500);
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//sets layout manager to arrange the panels
    		setLayout(new BorderLayout());
    		
    		build8BallPanel();
    		
    		addRadioButtons();
    		
    		//adds panels to specific areas of the screeen
    		add(panel1, BorderLayout.NORTH);
    		add(panel2, BorderLayout.EAST);
    		add(panel3, BorderLayout.WEST);
    		add(panel4, BorderLayout.SOUTH);
    		
    		pack();
    		
    		setVisible(true);
    	}
    	
    	private class RadioButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent radio)
    		{
    			if(eightBallradio.isSelected())
    			{
    				//calls method to build the 8Ball screen
    				build8BallPanel();
    			}
    			else if(fortuneradio.isSelected())
    			{
    				//calls method to build the 8Ball screen
    				buildFortunePanel();
    			}
    		}
    	}
    	private void addRadioButtons()
    	{
    		//Creates and Groups the radio buttons
    		eightBallradio = new JRadioButton("Magic 8 Ball", true);
    		fortuneradio = new JRadioButton("Fortune Teller");
    		
    		ButtonGroup group = new ButtonGroup();
    		
    		group.add(eightBallradio);
    		group.add(fortuneradio);
    		
    		//Create action listeners for the radio buttons
    		eightBallradio.addActionListener(new RadioButtonListener());
    		fortuneradio.addActionListener(new RadioButtonListener());
    		
    		panel4 = new JPanel();
    		
    		panel4.add(eightBallradio);
    		panel4.add(fortuneradio);	
    	}
    	
    	private void build8BallPanel()
    	{
    		//Create buttons and their action listeners
    		question = new JLabel("Got a question about your future?");
    		predictButton = new JButton("Your Future Now!");
    		predictButton.addActionListener(new PredictButtonListener());
    		
    		clearButton = new JButton("Clear");
    		clearButton.addActionListener(new ClearButtonListener());
    		
    		textArea = new JTextArea(rows, cols);
    		
    		panel1 = new JPanel();
    		panel2 = new JPanel();
    		panel3 = new JPanel();
    		
    		//Adds all the different components to the appropriate panel
    		panel1.add(question);
    		panel2.add(textArea);
    		panel3.add(predictButton);
    		panel3.add(clearButton);		
    	}
    	
    	private void buildFortunePanel()
    	{
    		//Create buttons and their action listeners
    		question = new JLabel("See the furture");
    		fortuneButton = new JButton("Your Future Now!");
    		fortuneButton.addActionListener(new FortuneButtonListener());
    		
    		clearButton = new JButton("Clear");
    		clearButton.addActionListener(new ClearButtonListener());
    		
    		textArea = new JTextArea(rows, cols);
    		
    		panel1 = new JPanel();
    		panel2 = new JPanel();
    		panel3 = new JPanel();
    		panel4 = new JPanel();
    		
    		//Adds all the different components to the appropriate panel
    		panel1.add(question);
    		panel2.add(textArea);
    		panel3.add(fortuneButton);
    		panel3.add(clearButton);		
    	}
    	
    	private class FortuneButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent fortune)
    		{
    			/*Declare and initiate String array.  String array will hold 
    			 * the */
    			String[] answers = new String[10];
    			answers[0] = "Always try your best" + "\n";
    			answers[1] = "Don't count on it" + "\n";
    			answers[2] = "In your dreams" + "\n";
    			answers[3] = "Answer currently unavailable" + "\n";
    			answers[4] = "Outlook is very hopefull" + "\n";
    			answers[5] = "Yes" + "\n";
    			answers[6] = "No" + "\n";
    			answers[7] = "Absolutely" + "\n";
    			answers[8] = "That is a definate possibility" + "\n";
    			answers[9] = "Don't see it happening" + "\n";
    			
    			Random randomNumbers = new Random(10);
    			int selection = randomNumbers.nextInt(10);
    			
    			textArea.append(answers[selection]);
    		}		
    	}
    	
    	private class PredictButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent predict)
    		{
    			/*Declare and initiate String array.  String array will hold 
    			 * the */
    			String[] answers = new String[10];
    			answers[0] = "Definately" + "\n";
    			answers[1] = "Don't count on it" + "\n";
    			answers[2] = "In your dreams" + "\n";
    			answers[3] = "Answer currently unavailable" + "\n";
    			answers[4] = "Outlook is very hopefull" + "\n";
    			answers[5] = "Yes" + "\n";
    			answers[6] = "No" + "\n";
    			answers[7] = "Absolutely" + "\n";
    			answers[8] = "That is a definate possibility" + "\n";
    			answers[9] = "Don't see it happening" + "\n";
    			
    			Random randomNumbers = new Random(10);
    			int selection = randomNumbers.nextInt(10);
    			
    			textArea.append(answers[selection]);
    		}
    	}
    	private class ClearButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent clear)
    		{
    			textArea.setText("");
    		}
    	}
    }
    here is the main ags

    Java Code:
    public class EightBall {
    	
    	public static void main(String[]args)
    	{
    		new EightBallWindow();
    	}	
    }
    Last edited by flameofSuzaku; 11-25-2007 at 10:06 PM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Fritzenbergerfx View Post
    Hi,
    My requirements are to pay the lowest taxes possible, will need bank account with internet banking plus a merchant card account to simply accept credit card payments for my Running shoes store.

    With thanks in advance.
    This is not a "Tax Consultants Forum" dude. You are in the wrong place.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Disable Radio button
    By AJG in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 11:09 AM
  2. Setting the label of radio buttons from Resource bundle
    By rajeeshankar in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 12-17-2007, 09:23 AM
  3. property for radio buttons
    By swapnanair in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-03-2007, 08:39 AM
  4. Radio button values are not lose
    By chockalingam in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-03-2007, 08:14 AM
  5. Next, Finish Buttons !!!
    By pele in forum SWT / JFace
    Replies: 1
    Last Post: 07-14-2007, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •