0

I am making a game, and I started working everything in one Class. But now I want to separate out in different classes. I am having one problem with the following code. I have made a JFrame in the GameView class, and half of the code its code i want to copy it to a different class and it cannot find variable frame because its in the GameView? Below is my code:

GameView Class:

public void gui()
    {
        BorderLayout borderlayout = new BorderLayout();      

        //Creates the frame, set the visibility to true, sets the size and exit on close
        JFrame frame = new JFrame("Games");
        frame.setVisible(true);
        frame.setSize(600,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Creates the Throw Dice button
        Panel p = new Panel();
        p.setLayout(new GridLayout());
        p.add(new Button("Throw Dice"));
        p.add(new Label("Dice Draw")); 
        p.setBackground(new Color(156, 93, 82));
        frame.add(p, BorderLayout.SOUTH);

Dice Class:

//Creates the Throw Dice button //MOVED FROM THE GameView CLASS

Panel p = new Panel();
p.setLayout(new GridLayout());
p.add(new Button("Throw Dice"));
p.add(new Label("Dice Draw")); 
p.setBackground(new Color(156, 93, 82));
frame.add(p, BorderLayout.SOUTH);// cannot find variable frame when i put this section into a different class

So I want the frame window to be in GameView class, and the dice part in a Dice class. But then its giving an error cannot find variable frame because its in the GameView class. How can I implement it?

user1329572
  • 6,176
  • 5
  • 29
  • 39
Umzz Mo
  • 219
  • 1
  • 6
  • 14

1 Answers1

2

You probably want something like this:

in your gui() method:

JFrame frame = new JFrame("Games");
...
frame.add(new Dice(), BorderLayout.SOUTH);

and the Dice class

public class Dice extends Panel {

    public Dice() {
        setLayout(new GridLayout());
        add(new Button("Throw Dice"));
        // add more stuff here
    }
...
Torious
  • 3,364
  • 17
  • 24