LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2011, 07:36 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Java Compile error


Hello guys;

I am working on this program for class and I am getting a strange error when trying to compile it. It keeps saying that a ] is expected on line 14 but I have a ] on line 14? I am sure this is something to do with accessing JLabel objects with an array but I am unsure.

Normally I would not ask here for class related stuff but this one has me stumped so any help or pointers to find the solution would be helpful.

Code:
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class JDemoResistance extends JFrame implements ActionListener
{

	JLabel[] option = new JLabel[6]; 
	final int WIDTH = 250; 
	final int HEIGHT = 120; 
	final String CLEAR = ""; 
	int count = 0; 
	JButton but1 = new JButton("Remove Option"); 
	option[5] = new JLabel("Remove all that do not apply"); 
	option[4] = new JLabel("Makes no sense");
	option[3] = new JLabel("Not Enough CREEPERS!");
	option[2] = new JLabel("Should include cat ninjas");
	option[1] = new JLabel("Too many bugs"); 
	option[0] = new JLabel("Too bright in dark areas"); 
	    
	public JDemoResistance()
	{
		super("JDemoResistance"); 
		setSize(WIDTH,HEIGHT); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setLayout(new FlowLayout()); 
		
		option[6].setFont(new Font("Arial",Font.BOLD,22); 
		for(int i = 5; i < 0, i--) //add all elements of the array to the Frame
		{
			add(option[i]); 
		}
		
		add(but1); // add button to the Frame 
		
		
		but1.addActionListener(this); // capture events and execute actionPerformed
		
		
		 
	}
	public static void main(String[] args) 
	{
	
		JDemoResistance myStuff = new JDemoResistance(); 
		myStuff.setVisible(true) 	
		 
	} 
	
	
	public void actionPerformed(ActionListener a)
	{
		if (count < 4) // prevent removing last item and informative label 
		{
			option[count].setText(CLEAR); // clear label at count number 
			
		}
		
		count++; 
		 
	}
}

Last edited by exvor; 10-06-2011 at 07:42 PM. Reason: Fixed some errors that I found, problem persists tho
 
Old 10-06-2011, 08:03 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Never mind I fixed the issue. I moved my elements that created the array elements and put them into the constructor for the class. It works this way and I think the reason is because the object has to be created before I can access the array elements but this may not be correct. Also I was unable to use the for loop to add the JLabel Objects to the JFrame object? This may be something thats discussed in the next chapter of my book I will have to see.... Below is the working code I settled with for the assignment which I will submit.

Code:
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class JDemoResistance extends JFrame implements ActionListener
{

	JLabel[] option = new JLabel[5]; 
	final int WIDTH = 250; 
	final int HEIGHT = 120; 
	final String CLEAR = ""; 
	int count = 0; 
	JButton but1 = new JButton("Remove Option"); 

	    
	public JDemoResistance()
	{
		super("JDemoResistance"); 
		setSize(WIDTH,HEIGHT); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setLayout(new FlowLayout()); 
		option[4] = new JLabel("Makes no sense");
	   option[3] = new JLabel("Not Enough CREEPERS!");
	   option[2] = new JLabel("Should include cat ninjas");
	   option[1] = new JLabel("Too many bugs"); 
	   option[0] = new JLabel("Too bright in dark areas"); 
		
		add(but1); // add button to the Frame 
		add(option[4]); 
		add(option[3]); 
		add(option[2]); 
		add(option[1]); 
		add(option[0]); 
		
	
		
		but1.addActionListener(this); // capture events and execute actionPerformed
		
		
		 
	}
	
	public void actionPerformed(ActionEvent a)
	{
		if (count < 4) // prevent removing last item and informative label 
		{
			option[count].setText(CLEAR); // clear label at count number 
			
		}
		
		count++; 
		 
	}
	public static void main(String[] args) 
	{
	
		JDemoResistance myStuff = new JDemoResistance(); 
		myStuff.setVisible(true); 	
		 
	} 
	
	
	
}

Last edited by exvor; 10-06-2011 at 08:05 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java ant compile error (can't find package) brgsousa Linux - Software 1 05-24-2010 08:46 AM
[Solved] HelloWorld.java compile error MrUmunhum Programming 2 02-28-2010 12:37 PM
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
cant compile java files in fedora 8.. java/ maranganesan Linux - Software 4 12-31-2007 12:21 PM
Java compile error using Linux Enochs Linux - Software 4 08-04-2007 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration