Hi guys,
In my Java Programming II class, we have to create a program that displays cars entering and exiting a parking lot. I created 50 JButtons (I know there is a more efficient way to write my code, but how?) and I'm trying to get the Buttons to display a .GIF image when pressed.
This is the error I'm getting:
There are 50 errors (button01, button 02 etc...) for each button. I just didn't feel like pasting all of them.F:\Parking.java:326: cannot find symbol
symbol : method add(javax.swing.ImageIcon)
location: class javax.swing.JButton
button01.add(splat);
This is my code:
/*Parking.java */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Parking extends JFrame implements ActionListener { //NUMERICAL DECLARATIONS double hourlyRate; double tow; double dailyRate; int timeIN = 0; int timeOUT = 0; int counter = 0; int numCars = 0; ImageIcon splat = new ImageIcon("images/middle.gif"); private JMenuBar mainBar = new JMenuBar(); private JMenu menu1 = new JMenu("File"); private JMenu menu2 = new JMenu("Parking"); private JMenuItem exit = new JMenuItem("Exit"); private JMenuItem profReport = new JMenuItem("Profit Report"); private JButton profitReport = new JButton("Profit Report"); private JButton towTruck = new JButton("Call Tow Truck"); private JTextField carNums = new JTextField(15); private JButton button01 = new JButton(""); private JButton button02 = new JButton(""); private JButton button03 = new JButton(""); private JButton button04 = new JButton(""); private JButton button05 = new JButton(""); private JButton button06 = new JButton(""); private JButton button07 = new JButton(""); private JButton button08 = new JButton(""); private JButton button09 = new JButton(""); private JButton button10 = new JButton(""); private JButton button11 = new JButton(""); private JButton button12 = new JButton(""); private JButton button13 = new JButton(""); private JButton button14 = new JButton(""); private JButton button15 = new JButton(""); private JButton button16 = new JButton(""); private JButton button17 = new JButton(""); private JButton button18 = new JButton(""); private JButton button19 = new JButton(""); private JButton button20 = new JButton(""); private JButton button21 = new JButton(""); private JButton button22 = new JButton(""); private JButton button23 = new JButton(""); private JButton button24 = new JButton(""); private JButton button25 = new JButton(""); private JButton button26 = new JButton(""); private JButton button27 = new JButton(""); private JButton button28 = new JButton(""); private JButton button29 = new JButton(""); private JButton button30 = new JButton(""); private JButton button31 = new JButton(""); private JButton button32 = new JButton(""); private JButton button33 = new JButton(""); private JButton button34 = new JButton(""); private JButton button35 = new JButton(""); private JButton button36 = new JButton(""); private JButton button37 = new JButton(""); private JButton button38 = new JButton(""); private JButton button39 = new JButton(""); private JButton button40 = new JButton(""); private JButton button41 = new JButton(""); private JButton button42 = new JButton(""); private JButton button43 = new JButton(""); private JButton button44 = new JButton(""); private JButton button45 = new JButton(""); private JButton button46 = new JButton(""); private JButton button47 = new JButton(""); private JButton button48 = new JButton(""); private JButton button49 = new JButton(""); private JButton button50 = new JButton(""); private JPanel panel01 = new JPanel(new GridLayout(5, 10, 1, 3)); private JPanel panel02 = new JPanel(new FlowLayout()); public void composeMenus() { setJMenuBar(mainBar); mainBar.add(menu1); mainBar.add(menu2); menu1.add(exit); //PANEL01 panel01.add(button01); panel01.add(button02); panel01.add(button03); panel01.add(button04); panel01.add(button05); panel01.add(button06); panel01.add(button07); panel01.add(button08); panel01.add(button09); panel01.add(button10); panel01.add(button11); panel01.add(button12); panel01.add(button13); panel01.add(button14); panel01.add(button15); panel01.add(button16); panel01.add(button17); panel01.add(button18); panel01.add(button19); panel01.add(button20); panel01.add(button21); panel01.add(button22); panel01.add(button23); panel01.add(button24); panel01.add(button25); panel01.add(button26); panel01.add(button27); panel01.add(button28); panel01.add(button29); panel01.add(button30); panel01.add(button31); panel01.add(button32); panel01.add(button33); panel01.add(button34); panel01.add(button35); panel01.add(button36); panel01.add(button37); panel01.add(button38); panel01.add(button39); panel01.add(button40); panel01.add(button41); panel01.add(button42); panel01.add(button43); panel01.add(button44); panel01.add(button45); panel01.add(button46); panel01.add(button47); panel01.add(button48); panel01.add(button49); panel01.add(button50); //PANEL02 panel02.add(profitReport); panel02.add(carNums); panel02.add(towTruck); } public void setColor() { exit.addActionListener(this); button01.setBackground(Color.DARK_GRAY); button02.setBackground(Color.DARK_GRAY); button03.setBackground(Color.DARK_GRAY); button04.setBackground(Color.DARK_GRAY); button05.setBackground(Color.DARK_GRAY); button06.setBackground(Color.DARK_GRAY); button07.setBackground(Color.DARK_GRAY); button08.setBackground(Color.DARK_GRAY); button09.setBackground(Color.DARK_GRAY); button10.setBackground(Color.DARK_GRAY); button11.setBackground(Color.DARK_GRAY); button12.setBackground(Color.DARK_GRAY); button13.setBackground(Color.DARK_GRAY); button14.setBackground(Color.DARK_GRAY); button15.setBackground(Color.DARK_GRAY); button16.setBackground(Color.DARK_GRAY); button17.setBackground(Color.DARK_GRAY); button18.setBackground(Color.DARK_GRAY); button19.setBackground(Color.DARK_GRAY); button20.setBackground(Color.DARK_GRAY); button21.setBackground(Color.DARK_GRAY); button22.setBackground(Color.DARK_GRAY); button23.setBackground(Color.DARK_GRAY); button24.setBackground(Color.DARK_GRAY); button25.setBackground(Color.DARK_GRAY); button26.setBackground(Color.DARK_GRAY); button27.setBackground(Color.DARK_GRAY); button28.setBackground(Color.DARK_GRAY); button29.setBackground(Color.DARK_GRAY); button30.setBackground(Color.DARK_GRAY); button31.setBackground(Color.DARK_GRAY); button32.setBackground(Color.DARK_GRAY); button33.setBackground(Color.DARK_GRAY); button34.setBackground(Color.DARK_GRAY); button35.setBackground(Color.DARK_GRAY); button36.setBackground(Color.DARK_GRAY); button37.setBackground(Color.DARK_GRAY); button38.setBackground(Color.DARK_GRAY); button39.setBackground(Color.DARK_GRAY); button40.setBackground(Color.DARK_GRAY); button41.setBackground(Color.DARK_GRAY); button42.setBackground(Color.DARK_GRAY); button43.setBackground(Color.DARK_GRAY); button44.setBackground(Color.DARK_GRAY); button45.setBackground(Color.DARK_GRAY); button46.setBackground(Color.DARK_GRAY); button47.setBackground(Color.DARK_GRAY); button48.setBackground(Color.DARK_GRAY); button49.setBackground(Color.DARK_GRAY); button50.setBackground(Color.DARK_GRAY); panel01.setBackground(Color.YELLOW); } public Parking() { composeMenus(); setColor(); addActionListeners(); setLayouts(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(540, 540); setVisible(true); } public void setLayouts() { setLayout(new BorderLayout()); add(panel01, BorderLayout.CENTER); add(panel02, BorderLayout.SOUTH); } public void addActionListeners() { profitReport.addActionListener(this); exit.addActionListener(this); button01.addActionListener(this); button02.addActionListener(this); button03.addActionListener(this); button04.addActionListener(this); button05.addActionListener(this); button06.addActionListener(this); button07.addActionListener(this); button08.addActionListener(this); button09.addActionListener(this); button10.addActionListener(this); button11.addActionListener(this); button12.addActionListener(this); button13.addActionListener(this); button14.addActionListener(this); button15.addActionListener(this); button16.addActionListener(this); button17.addActionListener(this); button18.addActionListener(this); button19.addActionListener(this); button20.addActionListener(this); button21.addActionListener(this); button22.addActionListener(this); button23.addActionListener(this); button24.addActionListener(this); button25.addActionListener(this); button26.addActionListener(this); button27.addActionListener(this); button28.addActionListener(this); button29.addActionListener(this); button30.addActionListener(this); button31.addActionListener(this); button32.addActionListener(this); button33.addActionListener(this); button34.addActionListener(this); button35.addActionListener(this); button36.addActionListener(this); button37.addActionListener(this); button38.addActionListener(this); button39.addActionListener(this); button40.addActionListener(this); button41.addActionListener(this); button42.addActionListener(this); button43.addActionListener(this); button44.addActionListener(this); button45.addActionListener(this); button46.addActionListener(this); button47.addActionListener(this); button48.addActionListener(this); button49.addActionListener(this); button50.addActionListener(this); } public void profitFrame() { ProfitFrame profitFrame = new ProfitFrame(); profitFrame.setSize(200, 200); profitFrame.setVisible(true); profitFrame.setLayout(new FlowLayout()); } public void carCounter() { counter++; numCars++; if(counter == 2) { counter = 0; } else if(counter == 1) { button01.add(splat); button02.add(splat); button03.add(splat); button04.add(splat); button05.add(splat); button06.add(splat); button07.add(splat); button08.add(splat); button09.add(splat); button10.add(splat); button11.add(splat); button12.add(splat); button13.add(splat); button14.add(splat); button15.add(splat); button16.add(splat); button17.add(splat); button18.add(splat); button19.add(splat); button20.add(splat); button21.add(splat); button22.add(splat); button23.add(splat); button24.add(splat); button25.add(splat); button26.add(splat); button27.add(splat); button28.add(splat); button29.add(splat); button30.add(splat); button31.add(splat); button32.add(splat); button33.add(splat); button34.add(splat); button35.add(splat); button36.add(splat); button37.add(splat); button38.add(splat); button39.add(splat); button40.add(splat); button41.add(splat); button42.add(splat); button43.add(splat); button44.add(splat); button45.add(splat); button46.add(splat); button47.add(splat); button48.add(splat); button49.add(splat); button50.add(splat); } else if(counter == 0) { //BUTTON SHOULD RESET - REMOVING THE .gif IMAGE button01.validate(); button02.validate(); button03.validate(); button04.validate(); button05.validate(); button06.validate(); button07.validate(); button08.validate(); button09.validate(); button10.validate(); button11.validate(); button12.validate(); button13.validate(); button14.validate(); button15.validate(); button16.validate(); button17.validate(); button18.validate(); button19.validate(); button20.validate(); button21.validate(); button22.validate(); button23.validate(); button24.validate(); button25.validate(); button26.validate(); button27.validate(); button28.validate(); button29.validate(); button30.validate(); button31.validate(); button32.validate(); button33.validate(); button34.validate(); button35.validate(); button36.validate(); button37.validate(); button38.validate(); button39.validate(); button40.validate(); button41.validate(); button42.validate(); button43.validate(); button44.validate(); button45.validate(); button46.validate(); button47.validate(); button48.validate(); button49.validate(); button50.validate(); } } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); Container con = getContentPane(); if(source == exit) { System.exit(0); } else if (source == profitReport) { profitFrame(); } } public static void main (String[] args) { Parking frame = new Parking(); } }
As I said before, I'm still a beginner, but I know there has to be an easier way to write something like this; perhaps an array and forloops? We've learned all about them; I'm just not sure how I would use them to change individual JButtons, as opposed to all 50 of them.
This is what we have to write:
Write a program that keeps track of cars entering a parking lot:
I also referenced the Profit Report class: here is that code.
- Time in/Time Out
- 50 Parking Spaces
- $2 per hour
- $12 per day option
- No Overnights
- Hours of Operation: 7AM to 11PM (24 hour clock maybe?)
- End of Day Profit Report
import javax.swing.*; public class ProfitFrame extends JFrame { final int width=350; final int height=350; final int width2=500; final int height2=500; public ProfitFrame() { super("GMoney Parking Profit Report"); setSize(width,height); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
ANY HELP WOULD BE MUCH APPRECIATED!!