Defective code for dice, help please?
I'm using NetBeans to create a dice program. As such, the entire code is automatically created, except for the following:
Quote:
private void rollActionPerformed(java.awt.event.ActionEvent evt) {
Random dice = new Random();
int throw = dice.nextInt(6) + 1;
number.setText(throw);
}
(I also put "import java.util.Random;" at the top of the program, just below package declaration)
My program (which has a GUI, which is why I used NetBeans) consists of a JFrame, 2 JLabels (one is called "title" and is the title of the program ["Virtual Dice"], and the other is called "number", and is where I try to set the random number the program generated.) and a JButton (titled "roll") which makes the program
Anyways, NetBeans tells me the last two lines are bad. The second to last one says " ';' expected/not a statement/illegal start of expression" (I have replaced line breaks with a slash in the above.) The last line is bad because "illegal start of expression/illegal start of expression".
So what do I need to do it to fix this program?
Peace,
Byron
P.S., here is the entire source code:
Quote:
/*
* virtualDice.java
*
* Created on February 16, 2008, 8:32 AM
*/
package virtualDice;
import java.util.Random;
/**
*
* @author Byron&Cheryl
*/
public class virtualDice extends javax.swing.JFrame {
/** Creates new form virtualDice */
public virtualDice() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
title = new javax.swing.JLabel();
number = new javax.swing.JLabel();
roll = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
title.setFont(new java.awt.Font("Tahoma", 0, 24));
title.setText("Virtual Dice");
title.setHorizontalTextPosition(javax.swing.SwingC onstants.CENTER);
number.setFont(new java.awt.Font("Tahoma", 0, 36));
number.setText("0");
roll.setText("Roll!");
roll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rollActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(160, 160, 160)
.addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(145, 145, 145)
.addComponent(roll))
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addComponent(title)))
.addContainerGap(116, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addComponent(title)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.UNRELATED)
.addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(roll)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void rollActionPerformed(java.awt.event.ActionEvent evt) {
Random dice = new Random();
int throw = dice.nextInt(6) + 1;
number.setText(throw);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new virtualDice().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel number;
private javax.swing.JButton roll;
private javax.swing.JLabel title;
// End of variables declaration
}