R
R
roman38472014-03-15 13:13:49
Java
roman3847, 2014-03-15 13:13:49

How to access a JPanel component in Java?

There is a MyFrame class, which describes the creation of a frame, and in this class the JPanel component is created.
And there is an Example class that creates a Button, and then I want to add that Button to the JPanel. But an error pops up: panel cannot be resolved. Where is the mistake?

class MyFrame extends JFrame
{
  public MyFrame()
  {
    setSize(300, 400);
    setTitle("Example");
    
    JPanel panel = new JPanel();
    panel.setBackground(Color.green);
    add(panel);
   }
}

class Example
{
  public Example()
  {
    JButton Button = new JButton("oooooo");
    panel.add(Button); //в этой строке показывает ошибку
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question