This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes New Approach to GridLayout problem, but still having compiling problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New Approach to GridLayout problem, but still having compiling problems" Watch "New Approach to GridLayout problem, but still having compiling problems" New topic
Author

New Approach to GridLayout problem, but still having compiling problems

Dianne Calhoun
Greenhorn

Joined: Sep 25, 2001
Posts: 29
Ok, I started all over and took a different approach. I can get the DrawsFrame.java to compile, but the MyGridLayout.java will not. It gives me the following errors.
[errors]
C:\myjava\assignment1\problem2>javac DrawsFrame.java
C:\myjava\assignment1\problem2>javac MyGridLayout.java
MyGridLayout.java:17: ';' expected
checkBox = new JCheckBox[5] ("","snap to grid", "","show grid","
");
^
MyGridLayout.java:18: ';' expected
textFieldTitles = new JTextField[5] ("", 0,"x: ", 3,"", 0,"y: ",
3,"", 0);
^
MyGridLayout.java:19: ';' expected
textField = new JTextField[5] ("", 0,"8", 4, "", 0,"9", 4, "", 0
);
^
MyGridLayout.java:20: ';' expected
buttons = new JButtons[5] ("ok", "","cancel", "","help");
^
MyGridLayout.java:30: ';' expected
g2.draw()
^
MyGridLayout.java:17: cannot resolve symbol
symbol : variable checkBox
location: class MyGridLayout
checkBox = new JCheckBox[5] ("","snap to grid", "","show grid","
");
^
MyGridLayout.java:18: cannot resolve symbol
symbol : variable textFieldTitles
location: class MyGridLayout
textFieldTitles = new JTextField[5] ("", 0,"x: ", 3,"", 0,"y: ",
3,"", 0);
^
MyGridLayout.java:19: cannot resolve symbol
symbol : variable textField
location: class MyGridLayout
textField = new JTextField[5] ("", 0,"8", 4, "", 0,"9", 4, "", 0
);
^
MyGridLayout.java:20: cannot resolve symbol
symbol : variable buttons
location: class MyGridLayout
buttons = new JButtons[5] ("ok", "","cancel", "","help");
^
MyGridLayout.java:20: cannot resolve symbol
symbol : class JButtons
location: class MyGridLayout
buttons = new JButtons[5] ("ok", "","cancel", "","help");
^
MyGridLayout.java:30: draw(java.awt.Shape) in java.awt.Graphics2D cannot be appl
ied to ()
g2.draw()
^
11 errors
C:\myjava\assignment1\problem2>
[/errors]
DrawsFrame.java
[javacode]
import java.awt.*;
import java.awt.color.*;
import javax.swing.*;
import java.awt.geom.*;

public class DrawsFrame extends JFrame {
public DrawsFrame() {
setTitle("Align");
setSize(WIDTH, HEIGHT);
// construct the vertical box 1
JCheckBox checkBox1 = new JCheckBox("snap to grid");
JCheckBox checkBox2 = new JCheckBox("show grid");
Box vbox1 = Box.createVerticalBox();
vbox1.add(Box.createVerticalStrut(SPACE));
vbox1.add(checkBox1);
vbox1.add(Box.createVerticalStrut(SPACE));
vbox1.add(checkBox2);
vbox1.add(Box.createVerticalStrut(SPACE));
//construct the vertical box 2
JLabel textLabel1 = new JLabel("x: ");
JLabel textLabel2 = new JLabel("y: ");
Box vbox2 = Box.createVerticalBox();
vbox2.add(Box.createVerticalStrut(SPACE));
vbox2.add(textLabel1);
vbox2.add(Box.createVerticalStrut(SPACE));
vbox2.add(textLabel2);
vbox2.add(Box.createVerticalStrut(SPACE));
//construct the 3rd vertical box
JTextField textField1 = new JTextField("8", 4);
textField1.setMaximumSize(textField1.getPreferredSize());
JTextField textField2 = new JTextField("9", 4);
textField2.setMaximumSize(textField2.getPreferredSize());
Box vbox3 = Box.createVerticalBox();
vbox3.add(Box.createVerticalStrut(SPACE));
vbox3.add(textField1);
vbox3.add(Box.createVerticalStrut(SPACE));
vbox3.add(textField2);
vbox3.add(Box.createVerticalStrut(SPACE));

//construst the 4th vertical box
JButton button1 = new JButton(" ok ");
JButton button2 = new JButton("cancel");
JButton button3 = new JButton("help ");
Box vbox4 = Box.createVerticalBox();
vbox4.add(button1);
vbox4.add(Box.createVerticalStrut(SPACE));
vbox4.add(button2);
vbox4.add(Box.createVerticalStrut(SPACE));
vbox4.add(button3);
// add the vertical boxes inside a hotizontal box
Box hbox = Box.createHorizontalBox();
hbox.add(vbox1);
hbox.add(vbox2);
hbox.add(vbox3);
hbox.add(vbox4);
Container contentPane = getContentPane();
contentPane.add(hbox, BorderLayout.CENTER);

}
public static final int WIDTH = 350;
public static final int HEIGHT = 200;
public static final int SPACE = 10;
}
[/javacode]
MyGridLayout.java
[javacode]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyGridLayout extends JPanel {
public void paintMyGridLayout (Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
setBackground(Color.white);
checkBox = new JCheckBox[5] ("","snap to grid", "","show grid","");
textFieldTitles = new JTextField[5] ("", 0,"x: ", 3,"", 0,"y: ", 3,"", 0);
textField = new JTextField[5] ("", 0,"8", 4, "", 0,"9", 4, "", 0);
buttons = new JButtons[5] ("ok", "","cancel", "","help");

setLayout(new GridLayout(3,5));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
g2.draw()

}
}
[/javacode]
MyGridLayoutTest.java
[javacode]
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
public class MyGridLayoutTest {
public static void main (String[] args) {
DrawsFrame frame = new DrawsFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

[/javacode]
Any ideas what I'm doing wrong?
------------------
Thanks, Dianne


Thanks, Dianne
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
buttons = new JButtons[5] ("ok", "","cancel", "","help");
All of your arrays are defined wrong. You cannot make the size and default at the same time. Here is an int example:
int[] array = {1,2,3};
Creates an int array with 3 elements.
In your case, the names must match what it is your JButton array is name JButtons. That would need some objects called JButtons.
ex:
buttonArray = new JButton[5];
You would then need to create and populate each array element.
buttonArray = {new JButton{"ok), new JButton{"cancel")};
Is more what you are trying to do.
Here is a discussion on arrays you might find helpful array
Good luck
Dianne Calhoun
Greenhorn

Joined: Sep 25, 2001
Posts: 29
I did get this to work good enough to turn in, and your comments on this problem really helped. Thanks a lot Paul.
------------------
Thanks, Dianne
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: New Approach to GridLayout problem, but still having compiling problems
 
Similar Threads
JFrame
How to Align JCheckBox
parsing from JTextField to double
GridLayout program errors
Layout manager problem (horizontal & vertical boxes)