Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

Error in Main

Hello everyone out there I hope someone can help me with this simple question!

Something is wrong with the main. Error cannot resolve symbol- constructor EightPointBlocks()

Get error can't recondnize



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EightPointBlocks extends JPanel implements ActionListener
{
     JCheckBox onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;

public EightPointBlocks(int ncols, int nrows)

     {
          EightPointBlocksArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, ncols));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("          8 Point Blocks");
          label.setForeground(Color.red);
          setBackground(Color.white);
          looprow : for (int row = 0; row < nrows; row++)
          {
               for (int col = 0; col < ncols; col++)
               {
                    int n = (20 * col + row + 1);
                    if (n > 50)
                    {
                         break looprow;
                    }
                    JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    //EightPointBlocksArrayList.add(onOffButton);
                    gridPanel.add(onOffButton);
               }
          }
     }


     public static void main(String[] args)
     {
          JFrame frame = new JFrame();
          frame.getContentPane().add(new EightPointBlocks());
          frame.setSize(400, 400);
          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public void actionPerformed(ActionEvent e)
{
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          int index = EightPointBlocksArrayList.indexOf(button) ;

          if (index > -1)
          {
               EightPointBlocksArrayList.remove(index); // removal of button
               EightPointBlocksArrayList.remove(index); // removal of timing.wav

               button.setDefaultColor();
          }
          else
          {
               EightPointBlocksArrayList.add(button);
               EightPointBlocksArrayList.add("timing.wav");
 
               button.setPressedColor();
          }
         
          System.out.println(EightPointBlocksArrayList);
}  

}

class JColoredButton extends JButton
{
     private Color foreground;
     private Color deafultForeground;
     private Color background;
     private Color defaultBackground;

     public JColoredButton(String label, Color background, Color foreground)
     {
          super(label);
          this.background = background;
          this.foreground = foreground;
          defaultBackground = getBackground();
          deafultForeground = getForeground();
     }
     
     public void setPressedColor()
     {
          setBackground(background);
          setForeground(foreground);
     }
     
     public void setDefaultColor()
     {
          setBackground(defaultBackground);
          setForeground(deafultForeground);
     }
}          
Avatar of Javatm
Javatm
Flag of Singapore image

This should solve the problem :

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EightPointBlocks extends JPanel implements ActionListener
{
     JCheckBox onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;
     int ncols, nrows;

public EightPointBlocks()

     {
          EightPointBlocksArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, ncols));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("          8 Point Blocks");
          label.setForeground(Color.red);
          setBackground(Color.white);
          looprow : for (int row = 0; row < nrows; row++)
          {
               for (int col = 0; col < ncols; col++)
               {
                    int n = (20 * col + row + 1);
                    if (n > 50)
                    {
                         break looprow;
                    }
                    JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    //EightPointBlocksArrayList.add(onOffButton);
                    gridPanel.add(onOffButton);
               }
          }
     }


     public static void main(String[] args)
     {
          JFrame frame = new JFrame();
          frame.getContentPane().add(new EightPointBlocks());
          frame.setSize(400, 400);
          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public void actionPerformed(ActionEvent e)
{

          JColoredButton button = (JColoredButton)e.getSource();

          int index = EightPointBlocksArrayList.indexOf(button) ;

          if (index > -1)
          {
               EightPointBlocksArrayList.remove(index); // removal of button
               EightPointBlocksArrayList.remove(index); // removal of timing.wav

               button.setDefaultColor();
          }
          else
          {
               EightPointBlocksArrayList.add(button);
               EightPointBlocksArrayList.add("timing.wav");

               button.setPressedColor();
          }

          System.out.println(EightPointBlocksArrayList);
}

}

class JColoredButton extends JButton
{
     private Color foreground;
     private Color deafultForeground;
     private Color background;
     private Color defaultBackground;

     public JColoredButton(String label, Color background, Color foreground)
     {
          super(label);
          this.background = background;
          this.foreground = foreground;
          defaultBackground = getBackground();
          deafultForeground = getForeground();
     }

     public void setPressedColor()
     {
          setBackground(background);
          setForeground(foreground);
     }

     public void setDefaultColor()
     {
          setBackground(defaultBackground);
          setForeground(deafultForeground);
     }
}

Hope that helps . . .
Javatm
Test it and if it gives you a runtime error
it is because the int ncols, nrows; doesnt not have any values yet.

So you need to specify the value like :

int ncols=2, nrows=2;

Hope that helps . . .
Javatm
When creating the EightPointBlocks instance

          frame.getContentPane().add(new EightPointBlocks());

 should be (for instance):

          frame.getContentPane().add(new EightPointBlocks(2,4));
 
> When creating the EightPointBlocks instance
> frame.getContentPane().add(new EightPointBlocks());
> should be (for instance):
> frame.getContentPane().add(new EightPointBlocks(2,4));

Which is like my 1st and 2nd comment :)


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EightPointBlocks extends JPanel implements ActionListener
{
     JCheckBox onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;
     int ncols=2, nrows=2;

public EightPointBlocks()

     {
          EightPointBlocksArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, ncols));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("          8 Point Blocks");
          label.setForeground(Color.red);
          setBackground(Color.white);
          looprow : for (int row = 0; row < nrows; row++)
          {
               for (int col = 0; col < ncols; col++)
               {
                    int n = (20 * col + row + 1);
                    if (n > 50)
                    {
                         break looprow;
                    }
                    JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    //EightPointBlocksArrayList.add(onOffButton);
                    gridPanel.add(onOffButton);
               }
          }
     }


     public static void main(String[] args)
     {
          JFrame frame = new JFrame();
          frame.getContentPane().add(new EightPointBlocks());
          frame.setSize(400, 400);
          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public void actionPerformed(ActionEvent e)
{

          JColoredButton button = (JColoredButton)e.getSource();

          int index = EightPointBlocksArrayList.indexOf(button) ;

          if (index > -1)
          {
               EightPointBlocksArrayList.remove(index); // removal of button
               EightPointBlocksArrayList.remove(index); // removal of timing.wav

               button.setDefaultColor();
          }
          else
          {
               EightPointBlocksArrayList.add(button);
               EightPointBlocksArrayList.add("timing.wav");

               button.setPressedColor();
          }

          System.out.println(EightPointBlocksArrayList);
}

}

class JColoredButton extends JButton
{
     private Color foreground;
     private Color deafultForeground;
     private Color background;
     private Color defaultBackground;

     public JColoredButton(String label, Color background, Color foreground)
     {
          super(label);
          this.background = background;
          this.foreground = foreground;
          defaultBackground = getBackground();
          deafultForeground = getForeground();
     }

     public void setPressedColor()
     {
          setBackground(background);
          setForeground(foreground);
     }

     public void setDefaultColor()
     {
          setBackground(defaultBackground);
          setForeground(deafultForeground);
     }
}
ASKER CERTIFIED SOLUTION
Avatar of Javatm
Javatm
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

Comment from Javatm
Date: 04/23/2004 10:32PM PDT
 Comment  


Or it can be :  This one fixed my last posted comment. I should of looked at the code better before i tried your last one.

Thanks,
Christopher
Javatm

I have never gotten an email from you. Why don't you get an email at Yahoo to then we will be able to email each other. email me when you do.
Yes of course my friend I'm very sorry I was busy but dont worry I'll send you e-mails.
I have an e-mail in yahoo but it contains a lot of spams and I dont know how to fix it.

You know friend I've always wanted to be a real Java Programmer but I have'nt
got any chance to prove it.

I applied for one but they are still reviewing it. How I wish I could be a real
programmer instead of being a tech support.

Thanks for your time . . .
Javatm