Results 1 to 4 of 4
  1. #1
    Morning-owl is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Unable to create a JLabel object

    I have written a class that I am unable to compile. According to the cmd certain lines are not statements, but I believe them to be correctly declared:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class JCompDemo extends JFrame {
    
    	public JCompDemo(){
    		Container c = getContentPane();
    		c.setLayout(new FlowLayout());
    		c.setBackground(Color.lightGray);
    		
    		JLabel 11 = new JLabel("Java ", JLabel.CENTER);
    //The line above is not considered to be a statement.
    		11.setOpaque(true);
    		11.setBackground(Color.white);
    		11.setForeground(Color.blue);
    		11.setFont(new Font("SansSerif", Font.BOLD, 24);
    		
    		JLabel 12 = new JLabel("is", JLabel.CENTER);
    //Neither this one, above.
    		12.setForeground(Color.black);
    		12.setFont(new Font("SansSerif", Font.PLAIN, 24);
    		12.setBorder(new EtchedBorder());
    		
    		JLabel 13 = new JLabel("great!", JLabel.CENTER);
    //Nor this one, above.
    		13.setBackground(11.getBackground);
    		13.setForeground(11.getForeground());
    		13.setFont(new Font("SansSerif", Font.ITALIC, 24));
    		13.setPreferredSize(new Dimension(100, 100));
    		13.setToolTipText("Y'all heard me.");
    		
    		c.add(11); c.add(12); c.add(13);
    		pack();
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    	
    	public static void main (String [] arg){
    		JCompDemo j = new JCompDemo();
    	}
    }
    I would like to know if there is something wrong with the lines I've commented above, or if the problem is due to something else.
    Thanks by advance. <3

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You can't start a variable name with a number. It really doesn't make sense to do this either because how can the compiler know when you're dealing with the variable or with a true number.

  3. #3
    Morning-owl is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks, that helped. I thought that numbers were unconventional names for variables, not illegal names.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Morning-owl View Post
    Thanks, that helped. I thought that numbers were unconventional names for variables, not illegal names.
    Here's what the JLS has to say about identifiers: 3.8 Identifiers

Similar Threads

  1. What is best object to create?
    By lam5442 in forum New To Java
    Replies: 1
    Last Post: 02-23-2011, 09:44 PM
  2. Unable to create and write files
    By DrKilljoy in forum New To Java
    Replies: 4
    Last Post: 09-05-2010, 12:55 AM
  3. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  4. Replies: 0
    Last Post: 04-11-2010, 11:14 PM
  5. Replies: 11
    Last Post: 09-01-2008, 10:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •