meikyu
meikyu

Reputation: 57

Adding panel from a different class to frame

I have 3 classes. The class Window is supposed to contain and set up GUI components. I want to add panel to frame, but the code below does not seem to work. This is the compilation error message:

Error: cannot find symbol

symbol: variable getPanel

location: variable myPanel of type Panel

class Window {
    Frame myFrame = new Frame();
    Panel myPanel = new Panel();

    void run() {
        myFrame.build();
        myPanel.build();
    }

    public static void main(String[] args) {
        (new Window()).run();
    }
}

class Frame {
    JFrame frame;

    Panel myPanel = new Panel();

    void build() {
        frame = new JFrame("Frame");

        frame.add(button, BorderLayout.SOUTH);
        frame.add(myPanel.getPanel); //compilation error: cannot find symbol

        frame.setSize(500, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class Panel extends Frame {
    private JPanel panel;

    void build() {
    panel = new JPanel();
    }

    public JPanel getPanel() {
        return panel;
    }
}

So how do I add panel to frame?

Upvotes: 2

Views: 1230

Answers (1)

Matt C
Matt C

Reputation: 4555

You are trying to call a method getPanel(), but you have tried to call it by simply typing getPanel.

You are missing the parenthesis, which indicates you are calling a method rather than just accessing a variable.

Change this line:

frame.add(myPanel.getPanel);

to this:

frame.add(myPanel.getPanel());

Also, in the future be sure to include the entire error message, not just pieces you find important. This will help us to quickly find the problem and therefore be able to help you sooner.

Upvotes: 1

Related Questions