Results 1 to 3 of 3
- 11-25-2007, 10:01 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 1
- Rep Power
- 0
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.
here is the main agsJava 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(""); } } }
Java Code:public class EightBall { public static void main(String[]args) { new EightBallWindow(); } }Last edited by flameofSuzaku; 11-25-2007 at 10:06 PM.
- 01-10-2011, 03:09 PM #2
-
Spam post deleted, spammer banned. Thread locked.
Similar Threads
-
Disable Radio button
By AJG in forum New To JavaReplies: 3Last Post: 05-10-2011, 11:09 AM -
Setting the label of radio buttons from Resource bundle
By rajeeshankar in forum JavaServer Faces (JSF)Replies: 0Last Post: 12-17-2007, 09:23 AM -
property for radio buttons
By swapnanair in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-03-2007, 08:39 AM -
Radio button values are not lose
By chockalingam in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-03-2007, 08:14 AM -
Next, Finish Buttons !!!
By pele in forum SWT / JFaceReplies: 1Last Post: 07-14-2007, 05:22 PM


LinkBack URL
About LinkBacks

.gif)

Bookmarks