Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在层次结构方法中实现我的代码?_Java_Swing_Refactoring_Jframe - Fatal编程技术网

Java 如何在层次结构方法中实现我的代码?

Java 如何在层次结构方法中实现我的代码?,java,swing,refactoring,jframe,Java,Swing,Refactoring,Jframe,我正在做一个游戏,我开始在一个班里做所有的事情。但现在我想分班上课。我对以下代码有一个问题。我在GameView类中创建了一个JFrame,其中一半的代码我想将其复制到另一个类中,但它找不到变量frame,因为它在GameView中?下面是我的代码: GameView类: public void gui() { BorderLayout borderlayout = new BorderLayout(); //Creates the fram

我正在做一个游戏,我开始在一个班里做所有的事情。但现在我想分班上课。我对以下代码有一个问题。我在GameView类中创建了一个JFrame,其中一半的代码我想将其复制到另一个类中,但它找不到变量frame,因为它在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类中,而骰子部分在骰子类中。但是它给出了一个错误,因为它在GameView类中,所以找不到可变帧。如何实现它?

您可能需要这样的功能:

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
    }
...
另见此。