Java GUI problem

  • From: "Gilbert Neiva" <gneiva@xxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 29 Nov 2010 23:14:24 -0700

I am making a GUI application with an array the has 10 components. The 
components represent an apartment suite. The application asks the user how many 
occupance are in each apartment.
I made two button handler classes. Here is the code for class ButtonHandler.

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

public class ButtonHandler implements ActionListener
{
 public void actionPerformed(ActionEvent event)
 {
 GuiApartment.apartment_size[GuiApartment.index] = 
Integer.parseInt(GuiApartment.inputField); 
 }
 }

Here is the code for class ButtonHandler1

import java.awt.event.*;

import javax.swing.*;


public class ButtonHandler1 implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

for (GuiApartment.index = 0, GuiApartment.index <= 
GuiApartment.apartment_size.length, GuiApartment.index++)

{

GuiApartment.apartment_occupance = 
GuiApartment.apartment_size[GuiApartment.index];

}


GuiApartment.occupance = GuiApartment.occupance + 
GuiApartment.apartment_occupance;

GuiApartment.userPane.add(GuiApartment.occupance);

GuiApartment.apartment_average = GuiApartment.apartment_occupance / 
GuiApartment.apartment_size.length;

GuiApartment.average = GuiApartment.average + GuiApartment.apartment_average;

GuiApartment.userPane.add(GuiApartment.average);


for (GuiApartment.index = GuiApartment.apartment_size.length - 1 && 
GuiApartment.above_average = 0, GuiApartment.apartment_size > 
GuiApartment.apartment_average, GuiApartment.index--)

{

if (GuiApartment.apartment_size[GuiApartment.index] > 
GuiApartment.apartment_average)

GuiApartment.above_average++;

}


GuiApartment.aboveAverage = GuiApartment.aboveAverage + 
GuiApartment.above_average;

GuiApartment.userPane.add(GuiApartment.aboveAverage);


for (GuiApartment.index = 0 && GuiApartment.below_average = 0, 
GuiApartment.apartment_size < GuiApartment.apartment_average, 
GuiApartment.index++)

{

if (GuiApartment.apartment_size[GuiApartment.index] < 
GuiApartment.apartment_average)

GuiApartment.below_average++;

}


GuiApartment.belowAverage = GuiApartment.belowAverage + 
GuiApartment.below_average;

GuiApartment.userPane.add(GuiApartment.belowAverage);

GuiApartment.userPane.remove(quit);

GuiApartment.userFrame.setVisible(true);

}

} // End of ButtonHandler1



Here is the code for the driver class GuiApartment



import java.util.Scanner; // Imports class Scanner
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GuiApartment
{
 // Field declarations
 static int apartment_occupance = 0; // Number of occupance in apartment

 // declare and instantiate array of apartments
 static int apartment_size[] = new int[10]; 
 static int index = 0; // Index of array
 static int average_size; // Average occupance in building
 static int above_average; // Number of apartments with above average occupance
 static int below_average; // Number of apartments with below average occupance

 // Declare labels and text field
 static JLabel head1 = new JLabel("Prompt", JLabel.CENTER);
 static JLabel head2 = new JLabel("Input field", JLabel.CENTER);
 static JLabel head3 = new JLabel("Occupance in apartment", JLabel.CENTER);
 static JLabel occupance = new JLabel("Number of people in the building is: ", 
JLabel.LEFT);
static JLabel average = new JLabel("Average number of people in building is: ", 
JLabel.LEFT); 
 static JLabel aboveAverage = new JLabel("Number of apartments with above 
average number of occupance is: ", JLabel.LEFT);
static JLabel belowAverage = new JLabel("Number of apartments with below 
average number of occupance is: ", JLabel.LEFT);
 static JTextField inputField = new JTextField(2);
 
 // Declare buttons
 static JButton store = new JButton("Store");
static JButton quit = new JButton("Quit");

public static void main(String[] args)
{
JFrame userFrame = new JFrame();
userFrame.setSize(200, 100);
userFrame.setLocation(100, 200);
Container userPane = userFrame.getContentPane();
userPane.setLayout(new GridLayout(4, 2));

userPane.add(head1);
userPane.add(head2);

for (index = 0, index <= apartment_size.length, index++)
 {
  userPane.add(head3);
 userPane.add(inputField);
store.addActionListener(new ButtonHandler()); 
userPane.add(store); 
 }

quit.addActionListener(new ButtonHandler1());
userPane.add(quit);
}
}


Here are the error messages when I try to compile everything.



C:\Users\user\Desktop\COMP268\Practice\GuiApartment\src>javac *.java
ButtonHandler1.java:8: not a statement
for (GuiApartment.index = 0, GuiApartment.index <= GuiApartment.apartmen
t_size.length, GuiApartment.index++)
^
ButtonHandler1.java:8: ';' expected
for (GuiApartment.index = 0, GuiApartment.index <= GuiApartment.apartmen
t_size.length, GuiApartment.index++)
^
ButtonHandler1.java:15: ')' expected
GuiApartment.apartment_average = GuiApartment.apartment_occupance / GuiApartment
.apartment_size.length;
^
ButtonHandler1.java:19: not a statement
for (GuiApartment.index = GuiApartment.apartment_size.length - 1 && GuiApartment
.above_average = 0, GuiApartment.apartment_size > GuiApartment.apartment_average
, GuiApartment.index--)
^
ButtonHandler1.java:19: ';' expected
for (GuiApartment.index = GuiApartment.apartment_size.length - 1 && GuiApartment
.above_average = 0, GuiApartment.apartment_size > GuiApartment.apartment_average
, GuiApartment.index--)
^
ButtonHandler1.java:28: illegal start of expression
for (GuiApartment.index = 0 && GuiApartment.below_average = 0, GuiApartment.apar
tment_size < GuiApartment.apartment_average, GuiApartment.index++)
^
ButtonHandler1.java:34: ')' expected
GuiApartment.belowAverage = GuiApartment.belowAverage + GuiApartment.below_avera
ge;
^
GuiApartment.java:43: not a statement
for (index = 0, index <= apartment_size.length, index++)
^
GuiApartment.java:43: ';' expected
for (index = 0, index <= apartment_size.length, index++)
^
GuiApartment.java:53: illegal start of expression
}
^
10 errors
C:\Users\user\Desktop\COMP268\Practice\GuiApartment\src>



Can someone help me with this issue? What am I doing wrong.



Gilbert Neiva


Other related posts: