• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GridLayout program errors

 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having problems getting my program to compile. Here is what I'm supposed to be doing
Instructions: Create the following GUI. You do not have to provide any functionality.
The first column has 2 check boxes. The second column has 2 input
text fields. The third column has 3 buttons. You may want to create
separate panels for each column and use GridLayout objects for the layout
managers of the panels.

|-------------------------------------------------|
| |
|Align |
| |
|-------------------------------------------------|
| |
| [ OK ] |
| [ ] Snap to Grid X: [8 ] |
| [ Cancel ] |
| [ ] Show Grid Y: [9 ] |
| [ Help ] |
| |
|-------------------------------------------------|
Here is my code for GridLayout.java
<javacode>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridLayout extends JFrame
{
private JButton buttons[];
private String names[] =
{ "Snap to Grid", "X:", "OK", "Show Grid", "Y", "Cancel", "Help"};
private Container container;
private GridLayout grid1;

//set up GUI
public GridLayout()
{
super( "GridLayout" );
//Set up layout
grid1 = new GridLayout( 3, 3 );
//get content pane and set its layout
container = getContentPane();
container.setLayout( grid1 );
//create and add buttons
buttons = new JButton[ names.length ];
for ( int count = 0; count < names.length; count++ )<br /> {<br /> buttons[ count ] = new JButton( names[ count ] );<br /> container.add( buttons[ count ] );<br /> }<br /> // set size<br /> setSize(400,150);<br /> setVisible( true );<br /> }<br /> //execute application<br /> public static void main( String args[] )<br /> {<br /> GridLayout application = new GridLayout();<br /> application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );<br /> }<br /> }<br /> </javacode><br /> <errors><br /> GridLayout.java:24: cannot resolve symbol<br /> symbol : constructor GridLayout (int,int)<br /> location: class GridLayout<br /> grid1 = new GridLayout( 3, 3 );<br /> ^<br /> GridLayout.java:28: setLayout(java.awt.LayoutManager) in java.awt.Container cann<br /> ot be applied to (GridLayout)<br /> container.setLayout( grid1 );<br /> ^<br /> 2 errors<br /> C:\myjava\assignment1\problem2>
</errors>
If someone could help me with what I'm doing wrong I would appreciate it alot.

------------------
Thanks, Dianne
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, my first guess is that you use the name GridLayout for your class. There's already a GridLayout class defined, so you've overridden it I assume (never tried to override a class). You define a default GridLayout constructor, and then try to access the GridLayout constructor which you have overridden giving it 2 arguments. Like I said, I don't know if you can even override the class name or not, but here's my suggestion:
Change the name of YOUR class to anything but GridLayout, like MyGridLayout, then your call to the GridLayout (int, int) constructor should work and the call where you send grid1 as an argument should work too.
That brings up a question from me, can you override another class name like that? or overload? not sure what that would be.
Hope it works...
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kinda like overriding, but hiding. I think the deal is that the JVM looks in the default package for *.class files before it looks in the java.lang package, so the effect is that it finds your class and doesn't even look for the other one.
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion, but It actually gives me the same errors, but just with the different name you suggested. any other ideas, I'm stumped.
<errors>
C:\myjava\assignment1\problem2>javac MyGridLayout.java
MyGridLayout.java:24: cannot resolve symbol
symbol : constructor MyGridLayout (int,int)
location: class MyGridLayout
grid1 = new MyGridLayout( 3, 3 );
^
MyGridLayout.java:28: setLayout(java.awt.LayoutManager) in java.awt.Container ca
nnot be applied to (MyGridLayout)
container.setLayout( grid1 );
^
2 errors
</errors>

------------------
Thanks, Dianne
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you change your class name, make sure to change all references to it inside of your program as well. In your program, change all GridLayout's to MyGridLayout's except for where you declare grid1 and where you define it. Aside from those 2, change the rest. And (of course Marilyn is always correct. I did some more research. Your GridLayout class if you keep the same name is put in the default package, where GridLayout is in the java.awt package, so they are different. I believe instead of changing your class name, you could also use the fully qualified name of the original GridLayout class, ie: java.awt.GridLayout . So in your case, you could declare:
java.awt.GridLayout grid1;
then define:
grid1 = new java.awt.GridLayout (3, 3);
and this should work also.
Personally I'd change the name of your class, but it's up to you.
Good Luck!
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my code again. I did change it everywhere, except the grid1. If it's still giving me the same error then what else could it be?
<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGridLayout extends JFrame
{
private JButton buttons[];
private String names[] =
{ "Snap to Grid", "X:", "OK", "Show Grid", "Y", "Cancel", "Help"};
private Container container;
private MyGridLayout grid1;

//set up GUI
public MyGridLayout()
{
super( "MyGridLayout" );
//Set up layout
grid1 = new MyGridLayout( 3, 3 );
//get content pane and set its layout
container = getContentPane();
container.setLayout( grid1 );
//create and add buttons
buttons = new JButton[ names.length ];
for ( int count = 0; count < names.length; count++ )
{
buttons[ count ] = new JButton( names[ count ] );
container.add( buttons[ count ] );
}
// set size
setSize(400,150);
setVisible( true );
}
//execute application
public static void main( String args[] )
{
MyGridLayout application = new MyGridLayout();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
</code>
This is only my seventh program I've had to write, and I'm trying to learn form examples as well as others to be able to understand what I'm doing. Sometimes this is so frustrating. If I could bother you again to check my code and see what I could be doing wrong, cause the name change didn't change the errors I was getting. thanks for helping.
------------------
Thanks, Dianne
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change these 2 lines in your program:
private MyGridLayout grid1;
-and-
grid1 = new MyGridLayout( 3, 3 );
-to-
private GridLayout grid1;
-and-
grid1 = new GridLayout (3, 3);
These lines my stay just GridLayout because you're using the GridLayout class defined in the java.awt package. It would not make sense to define grid1 as the type of the class you are designing. You want grid1 to be the layout that you will use for your JFrame, nothing else. Change these 2 lines and you're off and running.

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that the class that YOU are writting - MyGridLayout - is a JFrame not a GridLayout, and has the constructors of JFrame. Inside your frame you want a GridLayout Layout Manager. So when you create a variable you need to understand are you making the container (then use MyGridLayout) or are you creating a Layout Manager (then use GridLayout).
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I got those changes, but now it's giving me other errors.
[errors]
C:\myjava\assignment1\problem2>javac MyGridLayout.java
MyGridLayout.java:23: cannot resolve symbol
symbol : constructor GridLayout (int,int)
location: class GridLayout
grid1 = new GridLayout( 3, 3 );
^
MyGridLayout.java:27: setLayout(java.awt.LayoutManager) in java.awt.Container ca
nnot be applied to (GridLayout)
container.setLayout( grid1 );
^
.\GridLayout.java:24: cannot resolve symbol
symbol : constructor GridLayout (int,int)
location: class GridLayout
setLayout( new GridLayout( 3, 3 ));
^
.\GridLayout.java:28: setLayout(java.awt.LayoutManager) in java.awt.Container ca
nnot be applied to (GridLayout)
container.setLayout( grid1 );
^
4 errors
[/errors]

I'm not sure what it is telling me here, that it doesn't like the container and the new GridLayout?
------------------
Thanks, Dianne
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a GridLayout.class file laying around from before you changed your class name? Could be that that is getting picked up still.
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I deleted it, and tried compiling again, but it still gives me those errors. I didn't even think that could happen, I thought it would get over written every time you recompiled. Anyway, that is not the answer, cause my file doesn't compile to make a class file.
------------------
Thanks, Dianne
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I took the exact code you posted, copied and pasted it into my IDE and it compiled just fine. You may want to check that there's not a GridLayout.class file still looming around in your current directory like Cindy said.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did delete all the class files in there, but it still didn't work for me. So, I started the whole problem over, and posted it in a new thread. Could you please take a look at that code. Thanks
------------------
Thanks, Dianne
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your file still named GridLayout.java and not MyGridLayout.java? If it is GridLayout.java, change the name to MyGridLayout.java delete any GridLayout class files laying around and try it.
 
Dianne Calhoun
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to say thanks. My problem that I finally turned in looked more like the second post I did, and it worked. Thanks everyone again.
------------------
Thanks, Dianne
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it to work
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic