13 Replies Latest reply: Oct 17, 2007 8:44 PM by 807600 RSS

    Random Number Generator issues

    807600
      Ok so I have an issue. I'm supposed to create a GUI app that has a frame that contains both a button and a textfield. When the button is pressed then a random number between 1 and 10 should appear in the textfield. I've got an illegal start of expression on line 49 which is the last private void...
      So what have I done wrong?
      /*
       * randomnumber.java
      */
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.util.Random;
      
      public class randomnumber extends JFrame 
      {
      
           Random randomObject = new Random();
           
        // declare controls used
        static JButton startButton = new JButton();
        static JTextField startTextField = new JTextField();
      
        public static void main(String args[]) 
        {
          new randomnumber().show();
        }
      
        public randomnumber() 
        {
          // frame constructor
          setTitle("Random Number Generator");
          getContentPane().setLayout(new GridBagLayout());
      
          // add controls
          GridBagConstraints gridConstraints = new GridBagConstraints();
          startButton.setText("Generate a Number");
          gridConstraints.gridx = 0;
          gridConstraints.gridy = 0;
          getContentPane().add(startButton, gridConstraints);
          startButton.addActionListener(new ActionListener() 
          {
            public void actionPerformed(ActionEvent e) 
            {
              startButtonActionPerformed(e);
            }
          });
          startTextField.setText("");
          startTextField.setColumns(15);
          gridConstraints.gridx = 2;
          gridConstraints.gridy = 0;
          getContentPane().add(startTextField, new GridBagConstraints());
          
        
        private void startButtonActionPerformed(ActionEvent e)
        {
          myInt = randomObject.nextInt(11); 
          startTextField.setText(String.valueOf(myInt));
      
        }
      }
      Edited by: Hessmix on Oct 17, 2007 6:15 PM