Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2007, 11:01 PM
Member
 
Join Date: Nov 2007
Posts: 1
flameofSuzaku is on a distinguished road
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.

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

Code:
public class EightBall { public static void main(String[]args) { new EightBallWindow(); } }

Last edited by flameofSuzaku : 11-25-2007 at 11:06 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply

« TicTacToe | Search »

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable Radio button AJG New To Java 0 04-07-2008 09:37 PM
Setting the label of radio buttons from Resource bundle rajeeshankar JavaServer Faces 0 12-17-2007 10:23 AM
property for radio buttons swapnanair JavaServer Pages (JSP) and JSTL 2 12-03-2007 09:39 AM
Radio button values are not lose chockalingam JavaServer Pages (JSP) and JSTL 0 12-03-2007 09:14 AM
Next, Finish Buttons !!! pele SWT / JFace 1 07-14-2007 06:22 PM


All times are GMT +3. The time now is 12:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org