0

私はゲームを作っています、そして私は1つのクラスですべてを働き始めました。しかし今、私は異なるクラスに分けたいと思います。次のコードで1つの問題が発生しています。GameViewクラスでJFrameを作成しましたが、コードの半分を別のクラスにコピーしたいのですが、GameViewにあるため、可変フレームが見つかりませんか?以下は私のコードです:

GameViewクラス:

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

サイコロクラス:

//サイコロを投げるボタンを作成します//GameViewクラスから移動しました

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

したがって、フレームウィンドウをGameViewクラスに、ダイス部分をDiceクラスに配置する必要があります。ただし、GameViewクラスにあるため、エラーが発生しても可変フレームを見つけることができません。どうすれば実装できますか?

4

1 に答える 1

2

あなたはおそらくこのようなものが欲しいでしょう:

あなたのgui()方法で:

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

とダイスクラス

public class Dice extends Panel {

    public Dice() {
        setLayout(new GridLayout());
        add(new Button("Throw Dice"));
        // add more stuff here
    }
...
于 2012-04-13T23:33:11.223 に答える