i am currently working on a GUI for a program. i have created 100 jpanels which i have set opaque and colored blue. All of these blocks get an error which says that an identifier is expected for the lines which set them opaque and colors them, heres an example:

BoardDesigner.java:97: <identifier> expected
    Block25.setBackground(MyBlue);
                         ^
BoardDesigner.java:97: <identifier> expected
    Block25.setBackground(MyBlue);
                                ^

heres a bit of the code:

//creating the blue colour used in the JLabels
    Color MyBlue = new Color(0,0,255);

//Creation of the Block JLabels
    JLabel Block1 = new JLabel();
    Block1.setOpaque(true);
    Block1.setBackground(MyBlue);
    JLabel Block2 = new JLabel();
    Block2.setOpaque(true);
    Block2.setBackground(MyBlue);
    JLabel Block3 = new JLabel();
    Block3.setOpaque(true);
    Block3.setBackground(MyBlue);

anyone have an idea whats wrong?

Recommended Answers

All 9 Replies

I don't really see anything wrong. You said they are JPanels but they are JLabels. (You can set the background on either, so again, no problem). Do you mind posting your code?

Heres the entire java file so far

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;

class BoardDesigner extends JPanel //implements ActionListener
{

//creating the blue colour used in the JLabels
    Color MyBlue = new Color(0,0,255);

//Creation of the JButtons
    JButton addSub = new JButton("Add Submarine");
    JButton addAir = new JButton("Add Aircraft Carrier");
    JButton addDes = new JButton("Add Destroyer");
    JButton clear = new JButton("Clear Screen");
    JButton load = new JButton("Load File");
    JButton save = new JButton("Save File");

//Creation of the Block JLabels
    JLabel Block1 = new JLabel();
    Block1.setOpaque(true);
    Block1.setBackground(MyBlue);
    JLabel Block2 = new JLabel();
    Block2.setOpaque(true);
    Block2.setBackground(MyBlue);
    JLabel Block3 = new JLabel();
    Block3.setOpaque(true);
    Block3.setBackground(MyBlue);
    JLabel Block4 = new JLabel();
    Block4.setOpaque(true);
    Block4.setBackground(MyBlue);
    JLabel Block5 = new JLabel();
    Block5.setOpaque(true);
    Block5.setBackground(MyBlue);
    JLabel Block6 = new JLabel();
    Block6.setOpaque(true);
    Block6.setBackground(MyBlue);
    JLabel Block7 = new JLabel();
    Block7.setOpaque(true);
    Block7.setBackground(MyBlue);
    JLabel Block8 = new JLabel();
    Block8.setOpaque(true);
    Block8.setBackground(MyBlue);
    JLabel Block9 = new JLabel();
    Block9.setOpaque(true);
    Block9.setBackground(MyBlue);
    JLabel Block10 = new JLabel();
    Block10.setOpaque(true);
    Block10.setBackground(MyBlue);
    JLabel Block11 = new JLabel();
    Block11.setOpaque(true);
    Block11.setBackground(MyBlue);
    JLabel Block12 = new JLabel();
    Block12.setOpaque(true);
    Block12.setBackground(MyBlue);
    JLabel Block13 = new JLabel();
    Block13.setOpaque(true);
    Block13.setBackground(MyBlue);
    JLabel Block14 = new JLabel();
    Block14.setOpaque(true);
    Block14.setBackground(MyBlue);
    JLabel Block15 = new JLabel();
    Block15.setOpaque(true);
    Block15.setBackground(MyBlue);
    JLabel Block16 = new JLabel();
    Block16.setOpaque(true);
    Block16.setBackground(MyBlue);
    JLabel Block17 = new JLabel();
    Block17.setOpaque(true);
    Block17.setBackground(MyBlue);
    JLabel Block18 = new JLabel();
    Block18.setOpaque(true);
    Block18.setBackground(MyBlue);
    JLabel Block19 = new JLabel();
    Block19.setOpaque(true);
    Block19.setBackground(MyBlue);
    JLabel Block20 = new JLabel();
    Block20.setOpaque(true);
    Block20.setBackground(MyBlue);
    JLabel Block21 = new JLabel();
    Block21.setOpaque(true);
    Block21.setBackground(MyBlue);
    JLabel Block22 = new JLabel();
    Block22.setOpaque(true);
    Block22.setBackground(MyBlue);
    JLabel Block23 = new JLabel();
    Block23.setOpaque(true);
    Block23.setBackground(MyBlue);
    JLabel Block24 = new JLabel();
    Block24.setOpaque(true);
    Block24.setBackground(MyBlue);
    JLabel Block25 = new JLabel();
    Block25.setOpaque(true);
    Block25.setBackground(MyBlue);
    JLabel Block26 = new JLabel();
    Block26.setOpaque(true);
    Block26.setBackground(MyBlue);
    JLabel Block27 = new JLabel();
    Block27.setOpaque(true);
    Block27.setBackground(MyBlue);
    JLabel Block28 = new JLabel();
    Block28.setOpaque(true);
    Block28.setBackground(MyBlue);
    JLabel Block29 = new JLabel();
    Block29.setOpaque(true);
    Block29.setBackground(MyBlue);
    JLabel Block30 = new JLabel();
    Block30.setOpaque(true);
    Block30.setBackground(MyBlue);
    JLabel Block31 = new JLabel();
    Block31.setOpaque(true);
    Block31.setBackground(MyBlue);
    JLabel Block32 = new JLabel();
    Block32.setOpaque(true);
    Block32.setBackground(MyBlue);
    JLabel Block33 = new JLabel();
    Block33.setOpaque(true);
    Block33.setBackground(MyBlue);
    JLabel Block34 = new JLabel();
    Block34.setOpaque(true);
    Block34.setBackground(MyBlue);
    JLabel Block35 = new JLabel();
    Block35.setOpaque(true);
    Block35.setBackground(MyBlue);
    JLabel Block36 = new JLabel();
    Block36.setOpaque(true);
    Block36.setBackground(MyBlue);
    JLabel Block37 = new JLabel();
    Block37.setOpaque(true);
    Block37.setBackground(MyBlue);
    JLabel Block38 = new JLabel();
    Block38.setOpaque(true);
    Block38.setBackground(MyBlue);
    JLabel Block39 = new JLabel();
    Block39.setOpaque(true);
    Block39.setBackground(MyBlue);
    JLabel Block40 = new JLabel();
    Block40.setOpaque(true);
    Block40.setBackground(MyBlue);
    JLabel Block41 = new JLabel();
    Block41.setOpaque(true);
    Block41.setBackground(MyBlue);
    JLabel Block42 = new JLabel();
    Block42.setOpaque(true);
    Block42.setBackground(MyBlue);
    JLabel Block43 = new JLabel();
    Block43.setOpaque(true);
    Block43.setBackground(MyBlue);
    JLabel Block44 = new JLabel();
    Block44.setOpaque(true);
    Block44.setBackground(MyBlue);
    JLabel Block45 = new JLabel();
    Block45.setOpaque(true);
    Block45.setBackground(MyBlue);
    JLabel Block46 = new JLabel();
    Block46.setOpaque(true);
    Block46.setBackground(MyBlue);
    JLabel Block47 = new JLabel();
    Block47.setOpaque(true);
    Block47.setBackground(MyBlue);
    JLabel Block48 = new JLabel();
    Block48.setOpaque(true);
    Block48.setBackground(MyBlue);
    JLabel Block49 = new JLabel();
    Block49.setOpaque(true);
    Block49.setBackground(MyBlue);
    JLabel Block50 = new JLabel();
    Block50.setOpaque(true);
    Block50.setBackground(MyBlue);
    JLabel Block51 = new JLabel();
    Block51.setOpaque(true);
    Block51.setBackground(MyBlue);
    JLabel Block52 = new JLabel();
    Block52.setOpaque(true);
    Block52.setBackground(MyBlue);
    JLabel Block53 = new JLabel();
    Block53.setOpaque(true);
    Block53.setBackground(MyBlue);
    JLabel Block54 = new JLabel();
    Block54.setOpaque(true);
    Block54.setBackground(MyBlue);
    JLabel Block55 = new JLabel();
    Block55.setOpaque(true);
    Block55.setBackground(MyBlue);
    JLabel Block56 = new JLabel();
    Block56.setOpaque(true);
    Block56.setBackground(MyBlue);
    JLabel Block57 = new JLabel();
    Block57.setOpaque(true);
    Block57.setBackground(MyBlue);
    JLabel Block58 = new JLabel();
    Block58.setOpaque(true);
    Block58.setBackground(MyBlue);
    JLabel Block59 = new JLabel();
    Block59.setOpaque(true);
    Block59.setBackground(MyBlue);
    JLabel Block60 = new JLabel();
    Block60.setOpaque(true);
    Block60.setBackground(MyBlue);
    JLabel Block61 = new JLabel();
    Block61.setOpaque(true);
    Block61.setBackground(MyBlue);
    JLabel Block62 = new JLabel();
    Block62.setOpaque(true);
    Block62.setBackground(MyBlue);
    JLabel Block63 = new JLabel();
    Block63.setOpaque(true);
    Block63.setBackground(MyBlue);
    JLabel Block64 = new JLabel();
    Block64.setOpaque(true);
    Block64.setBackground(MyBlue);
    JLabel Block65 = new JLabel();
    Block65.setOpaque(true);
    Block65.setBackground(MyBlue);
    JLabel Block66 = new JLabel();
    Block66.setOpaque(true);
    Block66.setBackground(MyBlue);
    JLabel Block67 = new JLabel();
    Block67.setOpaque(true);
    Block67.setBackground(MyBlue);
    JLabel Block68 = new JLabel();
    Block68.setOpaque(true);
    Block68.setBackground(MyBlue);
    JLabel Block69 = new JLabel();
    Block69.setOpaque(true);
    Block69.setBackground(MyBlue);
    JLabel Block70 = new JLabel();
    Block70.setOpaque(true);
    Block70.setBackground(MyBlue);
    JLabel Block71 = new JLabel();
    Block71.setOpaque(true);
    Block71.setBackground(MyBlue);
    JLabel Block72 = new JLabel();
    Block72.setOpaque(true);
    Block72.setBackground(MyBlue);
    JLabel Block73 = new JLabel();
    Block73.setOpaque(true);
    Block73.setBackground(MyBlue);
    JLabel Block74 = new JLabel();
    Block74.setOpaque(true);
    Block74.setBackground(MyBlue);
    JLabel Block75 = new JLabel();
    Block75.setOpaque(true);
    Block75.setBackground(MyBlue);
    JLabel Block76 = new JLabel();
    Block76.setOpaque(true);
    Block76.setBackground(MyBlue);
    JLabel Block77 = new JLabel();
    Block77.setOpaque(true);
    Block77.setBackground(MyBlue);
    JLabel Block78 = new JLabel();
    Block78.setOpaque(true);
    Block78.setBackground(MyBlue);
    JLabel Block79 = new JLabel();
    Block78.setOpaque(true);
    Block79.setBackground(MyBlue);
    JLabel Block80 = new JLabel();
    Block80.setOpaque(true);
    Block80.setBackground(MyBlue);
    JLabel Block81 = new JLabel();
    Block81.setOpaque(true);
    Block81.setBackground(MyBlue);
    JLabel Block82 = new JLabel();
    Block82.setOpaque(true);
    Block82.setBackground(MyBlue);
    JLabel Block83 = new JLabel();
    Block83.setOpaque(true);
    Block83.setBackground(MyBlue);
    JLabel Block84 = new JLabel();
    Block84.setOpaque(true);
    Block84.setBackground(MyBlue);
    JLabel Block85 = new JLabel();
    Block85.setOpaque(true);
    Block85.setBackground(MyBlue);
    JLabel Block86 = new JLabel();
    Block86.setOpaque(true);
    Block86.setBackground(MyBlue);
    JLabel Block87 = new JLabel();
    Block87.setOpaque(true);
    Block87.setBackground(MyBlue);
    JLabel Block88 = new JLabel();
    Block88.setOpaque(true);
    Block88.setBackground(MyBlue);
    JLabel Block89 = new JLabel();
    Block89.setOpaque(true);
    Block89.setBackground(MyBlue);
    JLabel Block90 = new JLabel();
    Block90.setOpaque(true);
    Block90.setBackground(MyBlue);
    JLabel Block91 = new JLabel();
    Block91.setOpaque(true);
    Block91.setBackground(MyBlue);
    JLabel Block92 = new JLabel();
    Block92.setOpaque(true);
    Block92.setBackground(MyBlue);
    JLabel Block93 = new JLabel();
    Block93.setOpaque(true);
    Block93.setBackground(MyBlue);
    JLabel Block94 = new JLabel();
    Block94.setOpaque(true);
    Block94.setBackground(MyBlue);
    JLabel Block95 = new JLabel();
    Block95.setOpaque(true);
    Block95.setBackground(MyBlue);
    JLabel Block96 = new JLabel();
    Block96.setOpaque(true);
    Block96.setBackground(MyBlue);
    JLabel Block97 = new JLabel();
    Block97.setOpaque(true);
    Block97.setBackground(MyBlue);
    JLabel Block98 = new JLabel();
    Block98.setOpaque(true);
    Block98.setBackground(MyBlue);
    JLabel Block99 = new JLabel();
    Block99.setOpaque(true);
    Block99.setBackground(MyBlue);
    JLabel Block100 = new JLabel();
    Block100.setOpaque(true);
    Block100.setBackground(MyBlue);

    public BoardDesigner()
    {
    //Setting HomeControllers Layout
        this.setLayout(new BorderLayout());

    //Creation of JPanels
        JPanel buttonPanel = new JPanel();
        this.add(buttonPanel,BorderLayout.WEST);
        JPanel gridPanel = new JPanel();
        this.add(gridPanel,BorderLayout.EAST);
        gridPanel.setLayout(new GridLayout());
    }
}

If it helps im using the kate editor on a linux operating system

Your method calls need to be in some kind of method - maybe the constructor. You can't just have them lying around in the class definition like that.

James is right - you can create the JLabels in the class definition, but nothing further. In the future, you should use an array if you have any type of collection of Objects like you do above. For example, this code will do the same as yours does, but in a few lines:

JLabel[] blocks = new JLabel[100];
for (int i = 0; i < blocks.length; i++){
    blocks[i] = new JLabel();
    blocks[i].setOpaque(true);
    blocks[i].setBackground(MyBlue);
}

ps. Think about having an array (or ArrayList) of JLabels and a loop rather than repeating the same code 100 times

(sorry, parallel posting with BJSJC, says same thing)

thanks thats solved my problem

can anyone tell me how to add those JLabels stored in the array into a JPanel?

forget about that, ive worked it out now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.