Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: timer.start() required identifier?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default timer.start() required identifier?

    I'm trying to put a counterdown timer in my Minesweeper program, however, on line 23 in my MinesweeperGrid timer.start(); is giving me an identifier expected error? I've looked in the APIs and nothing is required in start.();? help?

    heres my code:
     1 import java.awt.*;
      2 import javax.swing.*;
      3 import java.awt.event.*;
      4 
      5 public class MinesweeperGrid extends JPanel
      6 {
      7 
      8         int currentTime = 0;
      9         JFrame frame;
     10         GridPanel[] grid;
     11         String currentDiff = "easy";
     12         JButton reset = new JButton ("Reset");
     13         JLabel  time = new JLabel ("Time");
     14         JLabel flag = new JLabel ("Flags");
     15 
     16         javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener()
     17         {
     18                 public void actionPerformed (ActionEvent event)
     19                 {
     20                         time.setLabel(Integer.toString(currentTime));
     21                 }
     22         });
     23         timer.start();
     24 
     25         public MinesweeperGrid(JFrame newFrame)
     26         {
     27                 frame = newFrame;
     28                 grid = new GridPanel[81];
     29                 frame.getContentPane().setLayout(new GridLayout(10,9,0,0));
     30 
     31                 frame.getContentPane().add(time);
     32                 frame.getContentPane().add(new JLabel());
     33                 frame.getContentPane().add(new JLabel());
     34                 frame.getContentPane().add(new JLabel());
     35                 frame.getContentPane().add(reset);
     36                 frame.getContentPane().add(new JLabel());
     37                 frame.getContentPane().add(new JLabel());
     38                 frame.getContentPane().add(new JLabel());
     39                 frame.getContentPane().add(flag);
     40 
     41                 for( int i = 0; i < 81; i++)
     42                 {
     43                         grid[i] = new GridPanel();
     44                         frame.getContentPane().add(grid[i]);
     45                 }
     46 
     47         }
     48 
     49         public void changeDiff(String newDiff)
     50         {
     51                 if (newDiff.equals("easy"))
     52                 {
     53                         GridPanel[] newGrid = new GridPanel[81];
     54                         frame.getContentPane().removeAll();
     55 
     56                         frame.getContentPane().setLayout(new GridLayout(10,9,0,0));
     57 
     58                         frame.getContentPane().add(time);
     59                         frame.getContentPane().add(new JLabel());
     60                         frame.getContentPane().add(new JLabel());
     61                         frame.getContentPane().add(new JLabel());
     62                         frame.getContentPane().add(reset);
     63                         frame.getContentPane().add(new JLabel());
     64                         frame.getContentPane().add(new JLabel());
     65                         frame.getContentPane().add(new JLabel());
     66                         frame.getContentPane().add(flag);
     67 
     68                         for( int i = 0; i < 81; i+69+)
     69                         {
     70                                 newGrid[i] = new GridPanel();
     71                                 frame.getContentPane().add(newGrid[i]);
     72                         }
     73                         frame.getContentPane().validate();
     74                         super.repaint();
     75                         currentDiff = "easy";
     76                 }
     77 
     78                 if (newDiff.equals("medium"))
     79                 {
     80 
     81                         GridPanel[] newGrid = new GridPanel[256];
     82                         frame.getContentPane().removeAll();
     83 
     84                         frame.getContentPane().setLayout(new GridLayout(17,16,0,0));
     85 
     86                         frame.getContentPane().add(time);
     87                         frame.getContentPane().add(new JLabel());
     88                         frame.getContentPane().add(new JLabel());
     89                         frame.getContentPane().add(new JLabel());
     90                         frame.getContentPane().add(new JLabel());
     91                         frame.getContentPane().add(new JLabel());
     92                         frame.getContentPane().add(new JLabel());
     93                         frame.getContentPane().add(new JLabel());
     94                         frame.getContentPane().add(reset);
     95                         frame.getContentPane().add(new JLabel());
     96                         frame.getContentPane().add(new JLabel());
     97                         frame.getContentPane().add(new JLabel());
     98                         frame.getContentPane().add(new JLabel());
     99                         frame.getContentPane().add(new JLabel());
    100                         frame.getContentPane().add(new JLabel());
    101                         frame.getContentPane().add(flag);
    102 
    103                         for( int i = 0; i < 256; i++)
    104                         {
    105                                 newGrid[i] = new GridPanel();
    106                                 frame.getContentPane().add(newGrid[i]);
    107                         }
    108                         frame.getContentPane().validate();
    109                         super.repaint();
    110                         currentDiff = "medium";
    111                 }
    112 
    113                 if (newDiff.equals("hard"))
    114                 {
    115 
    116                         GridPanel[] newGrid = new GridPanel[480];
    117                         frame.getContentPane().removeAll();
    118 
    119                         frame.getContentPane().setLayout(new GridLayout(25,20,0,0));
    120 
    121                         frame.getContentPane().add(time);
    122                         frame.getContentPane().add(new JLabel());
    123                         frame.getContentPane().add(new JLabel());
    124                         frame.getContentPane().add(new JLabel());
    125                         frame.getContentPane().add(new JLabel());
    126                         frame.getContentPane().add(new JLabel());
    127                         frame.getContentPane().add(new JLabel());
    128                         frame.getContentPane().add(new JLabel());
    129                         frame.getContentPane().add(new JLabel());
    130                         frame.getContentPane().add(new JLabel());
    131                         frame.getContentPane().add(reset);
    132                         frame.getContentPane().add(new JLabel());
    133                         frame.getContentPane().add(new JLabel());
    134                         frame.getContentPane().add(new JLabel());
    135                         frame.getContentPane().add(new JLabel());
    136                         frame.getContentPane().add(new JLabel());
    137                         frame.getContentPane().add(new JLabel());
    138                         frame.getContentPane().add(new JLabel());
    139                         frame.getContentPane().add(new JLabel());
    140                         frame.getContentPane().add(flag);
    141 
    142                         for( int i = 0; i < 480; i++)
    143                         {
    144                                 newGrid[i] = new GridPanel();
    145                                 frame.getContentPane().add(newGrid[i]);
    146                         }
    147                         frame.getContentPane().validate();
    148                         super.repaint();
    149                         currentDiff = "hard";
    150                 }
    151         }
    152 
    153         public class resetListener implements ActionListener
    154         {
    155                 public void actionPerformed (ActionEvent event)
    156                 {
    157                         changeDiff(currentDiff);
    158                 }
    159         }
    160         public class timeListener implements ActionListener
    161         {
    162                 public void actionPerformed (ActionEvent event)
    163                 {
    164                         currentTime++;
    165                         time.setText(Integer.toString(currentTime));
    166                         repaint();
    167                         System.out.println("hey, work!");
    168                 }
    169         }
    170 }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: timer.start() required identifier?

    The call to the timer.start() is not within a defined program element. If you want to start the timer upon construction, place it within the constructor. If you want to start it based upon a method call, place it within a defined method.

Similar Threads

  1. unique identifier
    By jack_nutt in forum Java Theory & Questions
    Replies: 25
    Last Post: June 20th, 2011, 06:27 AM
  2. ok i have tried everything but identifier expected keeps popping up!
    By knoxy5467 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: June 5th, 2011, 11:13 PM
  3. Identifier expected
    By Sphinx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 01:50 PM
  4. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 08:33 PM
  5. [SOLVED] Java error "Another <identifier> expected"
    By bruint in forum What's Wrong With My Code?
    Replies: 23
    Last Post: May 1st, 2009, 08:47 AM