I'm fairly new to the java world. I've been taking a Computer Science class this year with the focus on Java. I've had 1 1/2 semesters of experience with it. However, the class is moving too slow for me so I've been teaching myself the language.
I'm trying to learn to make my own simple GUIs on my own. I'm finding it extremely easy and extremely fun.
To practice with ActionListener and some of the components I made a simple program where you roll a dice. The program does a Math.random()*x where x is the number of sides of the dice they choose via radio buttons.
The program is supposed to append the roll to the JTextArea which is where the problem is. No matter what I try I continuously get a rather large error when I try to append text.
Here's my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DiceRoller{
public static void main(String[]args){
CreateGUI gui=new CreateGUI();
gui.go();
}
}
class CreateGUI implements ActionListener{
final String NEWLINE=new String("\n");
int dienum,roll;
JTextArea textArea;
JFrame frame;
JRadioButton six,eight,ten,twelve;
public void go(){
frame=new JFrame("Dice-Roller");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton six=new JRadioButton("6-Sided Die");
six.setActionCommand("6");
six.addActionListener(this);
JRadioButton eight=new JRadioButton("8-Sided Die");
eight.setActionCommand("8");
eight.addActionListener(this);
JRadioButton ten=new JRadioButton("10-Sided Die");
ten.setActionCommand("10");
ten.addActionListener(this);
JRadioButton twelve=new JRadioButton("12-sided Die");
twelve.setActionCommand("12");
twelve.addActionListener(this);
ButtonGroup group=new ButtonGroup();
group.add(six);
group.add(eight);
group.add(ten);
group.add(twelve);
JLabel label=new JLabel("Dice:");
JPanel radioPanel = new JPanel(new GridLayout(0,1));
radioPanel.setLayout(new BoxLayout(radioPanel,BoxLayout.Y_AXIS));
radioPanel.add(label);
radioPanel.add(six);
radioPanel.add(eight);
radioPanel.add(ten);
radioPanel.add(twelve);
JButton roll1=new JButton("Roll");
roll1.addActionListener(new RollAction());
JTextArea textArea=new JTextArea("Pick your dice then click roll.");
textArea.setEditable(false);
textArea.setLineWrap(true);
JScrollPane scrollPane=new JScrollPane(textArea);
frame.getContentPane().add(BorderLayout.EAST,radioPanel);
frame.getContentPane().add(BorderLayout.SOUTH,roll1);
frame.getContentPane().add(BorderLayout.CENTER,scrollPane);
frame.setBounds(100,100,400,200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
int command=Integer.parseInt(e.getActionCommand());
dienum=command;
}
class RollAction implements ActionListener{
public void actionPerformed(ActionEvent e){
String text;
roll=(int)(Math.random()*dienum)+1;
text="You rolled a "+roll+" on a "+dienum+"-Sided Die.";
textArea.append(NEWLINE + text);
}
}
}
Anyone have any suggestions on why this wouldn't work?
I did a System.out.print(text) and it came out just fine so I know the calculations are working.
Here's a picture of the error I get:
http://e.1asphost.com/muunster/error.jpg (332kb)
I'm stumped. My only guess is there's some rules for appending to JTextArea that I'm not aware of.