Kevin Schultz
Kevin Schultz

Reputation: 884

Compile error: "cannot find symbol"

I am writing a coin flipping program for a class. I am having a problem getting the text to center in each of the grids. The grid layout is 3 x 3 but the text "H" or "T" in each grid is left justified. Here is the code for that creates the grid.

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

class Lab3Panel extends JPanel {

    Lab3Panel() {
        setLayout(new GridLayout(3, 3, 1, 1));

        Lab3Label[] label = new Lab3Label[9];
        label.setHorizontalTextPosition(SwingConstants.CENTER);
        label.setVerticalTextPosition(SwingConstants.CENTER);

        for (int i = 0; i < 9; i++) {
            label[i] = new Lab3Label(i);
            add(label[i]);
        }
    }
}

The error I am getting is:

Lab3Panel.java:15: error: cannot find symbol label.setHorizontalTextPosition(SwingConstants.CENTER);
                                                  ^

Upvotes: 0

Views: 1172

Answers (1)

user2030471
user2030471

Reputation:

You're calling setHorizontalTextPosition method on the array object while you should be calling it on one of its entries.

Something like: label[0].setHorizontalTextPosition

But to be able to compile the above statement the class Lab3Label must define or inherit the method setHorizontalTextPosition from one of it's super classes.

I think this is what you need:

Lab3Label[] label = new Lab3Label[9];
for (int i = 0; i < 9; i++) {
    label[i] = new Lab3Label(i);
    label[i].setHorizontalTextPosition(SwingConstants.CENTER);
    label[i].setVerticalTextPosition(SwingConstants.CENTER);
    add(label[i]);
}

Upvotes: 5

Related Questions