我无法从 class 调用代码

I am having trouble calling code from a class

所以我是 java 的新手,我正在为一款简单的扫雷游戏开发图形用户界面。最初我将所有代码集成到一个 class 中,并在同一个 class 中实例化了我的面板和框架,但我的教授坚持要我将框架和面板放在单独的 class 中。我现在被困在如何在我的框架中实现我的面板上。我试图在我的主要方法中创建 class 的实例,但是当我 运行 我的程序时它只显示一个空框。在我完美地设置单独的 classes 我的代码 运行 之前,但现在我的面板没有被添加到我的框架中。我的面板中是否遗漏了什么? 到目前为止,这是我的代码:

import java.awt.event.InputEvent;
import java.awt.*;
import java.util.Random;
import javax.swing.*;

public class Minesweeper 
{

        public static void main(String[] args)
        {


            Frame frame = new Frame();

            frame.getContentPane().add(new Panel());
            frame.setVisible(true);

        }
}

这只是框架。

    class Frame extends JFrame
    {
        public Frame()
        {

            setTitle("Minesweeper");
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    }

这是我的面板class我遇到了问题。

    class Panel extends JPanel 
    {
        public Panel() 
            {
                super(); 
                this.setLayout(new BorderLayout());
                add(getJPanel());
                add(getJContentPane());

            }

            private int columns = 8;
            private int rows = 8;
            boolean jBombs[][] = new boolean[rows][columns];
            boolean jShown[][] = new boolean[rows][columns];
            int jCells[][] = new int[rows][columns];
            private int currX, currY = 0;
            JToggleButton jButtons[] = new JToggleButton[columns*rows];
            private JPanel jPanel = null;
            private JToolBar jToolBar = null;
            private JPanel jContentPane = null;
            private JButton jBtnNewGame = null;
            private JProgressBar jProgressBar = null;






            private JPanel getJPanel()
            {
                if (jPanel == null)
                {
                        jPanel = new JPanel();
                        jPanel.setLayout(new BorderLayout());
                        jPanel.add(getJContentPane(), BorderLayout.CENTER);
                        jPanel.add(getJProgressBar(), BorderLayout.SOUTH);

                }
                return jPanel;
            }



            private JPanel getJContentPane() 
            {
                if (jContentPane == null)
                {
                        GridLayout gridLayout = new GridLayout();
                        gridLayout.setRows(rows);
                        gridLayout.setColumns(columns);
                        jContentPane = new JPanel();
                        jContentPane.setLayout(gridLayout);
                        BuildBoard();
                }
                return jContentPane;
            }

            private void BuildBoard()
            {

                if(jProgressBar != null)
                {
                        jProgressBar.setValue(0);
                }
                jContentPane.removeAll();
                int i = 0;
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                currX = x;
                                currY = y;
                                Random randBomb = new Random();
                                jBombs[x][y] = randBomb.nextBoolean() && randBomb.nextBoolean() && randBomb.nextBoolean();
                                jButtons[i] = new JToggleButton("?");
                                jButtons[i].addMouseListener(new java.awt.event.MouseAdapter(){
                                        public void mouseReleased(java.awt.event.MouseEvent e) {
                                                if(e.getModifiers() == InputEvent.BUTTON3_MASK)
                                                {
                                                        markCell(e);
                                                }
                                                else if(e.getModifiers() == InputEvent.BUTTON1_MASK)
                                                {
                                                        showCell(e);
                                                }
                                        }
                                });
                                jContentPane.add(jButtons[i]);
                                i++;
                        }
                }
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                jCells[x][y] = bombCount(x, y);
                                jShown[x][y] = false; 
                        }
                }
                jContentPane.setEnabled(true);
                this.repaint();
                this.validate();
            }

            private JProgressBar getJProgressBar()
            {
                if (jProgressBar == null) 
                {
                        jProgressBar = new JProgressBar();
                        jProgressBar.setMaximum(columns * rows);
                }
                return jProgressBar;
            }


            private void showAllBombs()
            {
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                if(jBombs[x][y] == true)
                                {
                                        JToggleButton jButton = findButton(x,y);
                                        if(jButton.isEnabled()) 
                                        {
                                                jProgressBar.setValue(jProgressBar.getValue() + 1);
                                        }
                                        jButton.setText("X");
                                        jButton.setSelected(true);
                                        jButton.setEnabled(false);
                                }
                        }
                }
            }

            private void clearCells(int x, int y)
            {
                if(inBounds(x, y))
                {
                        if(!jShown[x][y] && jBombs[x][y] == false)
                        {
                                jShown[x][y] = true;
                                JToggleButton jButton = findButton(x,y);
                                if(jCells[x][y] > 0)
                                {
                                        jButton.setText(Integer.toString(jCells[x][y]));
                                }
                                else
                                {
                                        jButton.setText("");
                                }
                                if(jButton.isEnabled())
                                {
                                        jProgressBar.setValue(jProgressBar.getValue() + 1);
                                }
                                jButton.setSelected(true);
                                jButton.setEnabled(false);
                                if(jCells[x][y] == 0)
                                {
                                        for(int r = -1; r <= 1; r++)
                                        {
                                                for(int c = -1; c <= 1; c++)
                                                {
                                                        clearCells(x + r, y + c);
                                                }
                                        }
                                }
                        }
                }
            }

            private boolean inBounds(int x, int y)
            {
                return 0 <= x && x < jCells.length && 0 <= y && y < jCells[x].length;
            }

            private boolean isBomb(JToggleButton jButton)
            {
                int i = 0;
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                if(jButton == jButtons[i])
                                {
                                        currX = x;
                                        currY = y;
                                        return jBombs[x][y];
                                }
                                i++;
                        }
                }
                return false;
            }

            private void disableBoard()
            {
                for(int x = 0; x < rows; x++)
                {
                        for(int y = 0; y < columns; y++)
                        {
                                JToggleButton jButton = findButton(x,y);
                                jButton.setEnabled(false);
                        }
                }
            }

            private JToggleButton findButton(int x, int y)
            {
                return jButtons[(x*rows+y)];
            }

            private void showCell(java.awt.event.MouseEvent e)
            {
                JToggleButton jButton = (JToggleButton)e.getSource();
                if(jButton.isEnabled())
                {
                        jProgressBar.setValue(jProgressBar.getValue() + 1);
                        jButton.setEnabled(false);

                        if(isBomb(jButton))
                        {
                                showAllBombs();
                                jButton.setEnabled(false);
                                JOptionPane.showMessageDialog(null,"You lost " + Math.round((jProgressBar.getPercentComplete() * 100)) + "% through.", "You Lost!", JOptionPane.INFORMATION_MESSAGE);
                                disableBoard();
                                System.exit(0);
                        }

                        else
                        {
                                if(jCells[currX][currY] > 0)
                                {
                                        jButton.setText(Integer.toString(jCells[currX][currY]));
                                }
                                else if(jCells[currX][currY] == 0)
                                {
                                        clearCells(currX, currY);
                                }

                        }
                }
            }

            private int bombCount(int x, int y)
            {
                int bombCount = 0;
                for(int r = -1; r <= 1; r++)
                {
                        for(int c = -1; c <= 1; c++)
                        {
                                int newx = x + r;
                                int newy = y + c;
                                if(inBounds(newx, newy))
                                {
                                        if(jBombs[newx][newy] == true)
                                        {
                                                bombCount++;
                                        }
                                }
                        }
                }
                return bombCount;
            }

            private void markCell(java.awt.event.MouseEvent e) 
            {
                JToggleButton jButton = (JToggleButton)e.getSource();
                if(jButton.isEnabled())
                {
                        if(jButton.getText() != "!")
                        {
                                jButton.setText("!");                          
                        }
                        else
                        {
                                jButton.setText("");
                        }
                }
            }

    }

您实际上从未调用任何会添加(或生成)您的 Panel class.

内容的内容

在您的 Panel 构造函数中,尝试调用 getJPanelgetJContentPane 并将这些面板实际添加到您的 Panel class.

另外,这个...

Panel panel = new Panel();
Frame frame = new Frame();

frame.getContentPane().add(new Panel());

没有意义,您创建了两个实例 Panel,但只使用了一个。你可以只使用

public static void main(String[] args) {

    Frame frame = new Frame();

    frame.getContentPane().add(new Panel());
    //...

}

相反。

另外,不要使用...

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width;
int height = screenSize.height;
setSize(width, height);

这可能会导致您的框架显示在其他 OS 元素(即任务栏)下方,您应该使用...

setExtendedState(JFrame.MAXIMIZED_BOTH);

相反。

如果您将 classes 拆分到单独的文件中(比将其保存在单个文件中更好的做法),您必须像这样导入 classes:

假设您的 class 都在同一个文件夹中

Minesweeper.java

import java.awt.event.InputEvent;
import java.awt.*;
import java.util.Random;
import javax.swing.*;
import MyPanel;
import MyFrame;

public class Minesweeper 
{

        public static void main(String[] args)
        {

            Frame frame = new MyFrame();

            frame.getContentPane().add(new MyPanel());
            frame.setVisible(true);

        }
}

Frame.java

import javax.swing.*;
public class MyFrame extends JFrame
{
    public MyFrame()
    {
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setTitle("Minesweeper");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Panel.java

import javax.swing.*;
public class MyPanel extends JPanel 
{
    private int columns = 8;
    private int rows = 8;
    boolean jBombs[][] = new boolean[rows][columns];
    boolean jShown[][] = new boolean[rows][columns];
    int jCells[][] = new int[rows][columns];
    private int currX, currY = 0;
    JToggleButton jButtons[] = new JToggleButton[columns*rows];
    private JPanel jPanel = null;
    private JToolBar jToolBar = null;
    private JPanel jContentPane = null;
    private JButton jBtnNewGame = null;
    private JProgressBar jProgressBar = null;

    public MyPanel() 
    {
        super();

    }

    private JPanel getJPanel()
    {
        if (jPanel == null)
        {
                jPanel = new JPanel();
                jPanel.setLayout(new BorderLayout());
                jPanel.add(getJContentPane(), BorderLayout.CENTER);
                jPanel.add(getJProgressBar(), BorderLayout.SOUTH);

        }
        return jPanel;
    }

    private JPanel getJContentPane() 
    {
        if (jContentPane == null)
        {
                GridLayout gridLayout = new GridLayout();
                gridLayout.setRows(rows);
                gridLayout.setColumns(columns);
                jContentPane = new JPanel();
                jContentPane.setLayout(gridLayout);
                BuildBoard();
        }
        return jContentPane;
    }

    private void BuildBoard()
    {

        if(jProgressBar != null)
        {
                jProgressBar.setValue(0);
        }
        jContentPane.removeAll();
        int i = 0;
        for(int x = 0; x < rows; x++)
        {
                for(int y = 0; y < columns; y++)
                {
                        currX = x;
                        currY = y;
                        Random randBomb = new Random();
                        jBombs[x][y] = randBomb.nextBoolean() && randBomb.nextBoolean() && randBomb.nextBoolean();
                        jButtons[i] = new JToggleButton("?");
                        jButtons[i].addMouseListener(new java.awt.event.MouseAdapter(){
                                public void mouseReleased(java.awt.event.MouseEvent e) {
                                        if(e.getModifiers() == InputEvent.BUTTON3_MASK)
                                        {
                                                markCell(e);
                                        }
                                        else if(e.getModifiers() == InputEvent.BUTTON1_MASK)
                                        {
                                                showCell(e);
                                        }
                                }
                        });
                        jContentPane.add(jButtons[i]);
                        i++;
                }
        }
        for(int x = 0; x < rows; x++)
        {
                for(int y = 0; y < columns; y++)
                {
                        jCells[x][y] = bombCount(x, y);
                        jShown[x][y] = false; 
                }
        }
        jContentPane.setEnabled(true);
        this.repaint();
        this.validate();
    }

    private JProgressBar getJProgressBar()
    {
        if (jProgressBar == null) 
        {
                jProgressBar = new JProgressBar();
                jProgressBar.setMaximum(columns * rows);
        }
        return jProgressBar;
    }


    private void showAllBombs()
    {
        for(int x = 0; x < rows; x++)
        {
                for(int y = 0; y < columns; y++)
                {
                        if(jBombs[x][y] == true)
                        {
                                JToggleButton jButton = findButton(x,y);
                                if(jButton.isEnabled()) 
                                {
                                        jProgressBar.setValue(jProgressBar.getValue() + 1);
                                }
                                jButton.setText("X");
                                jButton.setSelected(true);
                                jButton.setEnabled(false);
                        }
                }
        }
    }

    private void clearCells(int x, int y)
    {
        if(inBounds(x, y))
        {
                if(!jShown[x][y] && jBombs[x][y] == false)
                {
                        jShown[x][y] = true;
                        JToggleButton jButton = findButton(x,y);
                        if(jCells[x][y] > 0)
                        {
                                jButton.setText(Integer.toString(jCells[x][y]));
                        }
                        else
                        {
                                jButton.setText("");
                        }
                        if(jButton.isEnabled())
                        {
                                jProgressBar.setValue(jProgressBar.getValue() + 1);
                        }
                        jButton.setSelected(true);
                        jButton.setEnabled(false);
                        if(jCells[x][y] == 0)
                        {
                                for(int r = -1; r <= 1; r++)
                                {
                                        for(int c = -1; c <= 1; c++)
                                        {
                                                clearCells(x + r, y + c);
                                        }
                                }
                        }
                }
        }
    }

    private boolean inBounds(int x, int y)
    {
        return 0 <= x && x < jCells.length && 0 <= y && y < jCells[x].length;
    }

    private boolean isBomb(JToggleButton jButton)
    {
        int i = 0;
        for(int x = 0; x < rows; x++)
        {
                for(int y = 0; y < columns; y++)
                {
                        if(jButton == jButtons[i])
                        {
                                currX = x;
                                currY = y;
                                return jBombs[x][y];
                        }
                        i++;
                }
        }
        return false;
    }

    private void disableBoard()
    {
        for(int x = 0; x < rows; x++)
        {
                for(int y = 0; y < columns; y++)
                {
                        JToggleButton jButton = findButton(x,y);
                        jButton.setEnabled(false);
                }
        }
    }

    private JToggleButton findButton(int x, int y)
    {
        return jButtons[(x*rows+y)];
    }

    private void showCell(java.awt.event.MouseEvent e)
    {
        JToggleButton jButton = (JToggleButton)e.getSource();
        if(jButton.isEnabled())
        {
                jProgressBar.setValue(jProgressBar.getValue() + 1);
                jButton.setEnabled(false);

                if(isBomb(jButton))
                {
                        showAllBombs();
                        jButton.setEnabled(false);
                        JOptionPane.showMessageDialog(null,"You lost " + Math.round((jProgressBar.getPercentComplete() * 100)) + "% through.", "You Lost!", JOptionPane.INFORMATION_MESSAGE);
                        disableBoard();
                        System.exit(0);
                }

                else
                {
                        if(jCells[currX][currY] > 0)
                        {
                                jButton.setText(Integer.toString(jCells[currX][currY]));
                        }
                        else if(jCells[currX][currY] == 0)
                        {
                                clearCells(currX, currY);
                        }

                }
        }
    }

    private int bombCount(int x, int y)
    {
        int bombCount = 0;
        for(int r = -1; r <= 1; r++)
        {
                for(int c = -1; c <= 1; c++)
                {
                        int newx = x + r;
                        int newy = y + c;
                        if(inBounds(newx, newy))
                        {
                                if(jBombs[newx][newy] == true)
                                {
                                        bombCount++;
                                }
                        }
                }
        }
        return bombCount;
    }

    private void markCell(java.awt.event.MouseEvent e) 
    {
        JToggleButton jButton = (JToggleButton)e.getSource();
        if(jButton.isEnabled())
        {
                if(jButton.getText() != "!")
                {
                        jButton.setText("!");                          
                }
                else
                {
                        jButton.setText("");
                }
        }
    }

}

注意:我只是复制导入,你检查它们是否是你需要的每个 class。