oolongtea oolongtea - 9 months ago 40
Java Question

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() {

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);

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?


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:


to this:


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.