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.
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
public class EightBall {
public static void main(String[]args)
{
new EightBallWindow();
}
}