如何在Java.Swing中重置Minesweeper游戏板

时间:2014-10-29 12:55:11

标签: java eclipse swing class reset

我一直试图让我的代码在用滑块点击okay按钮后重置,但是,我没有运气。有没有人知道如何将其合并到我的代码中?

P.S。重置必须在类字段中。

package GUI;

import java.awt.GridLayout;
import javax.swing.JPanel;

public class GameDisplay extends JPanel {
    private static final long serialVersionUID = 1L;
    private boolean activated;

    public GameDisplay(Model.Field field) {
        setLayout(new GridLayout(GUI.SizeDialog.currentRows, GUI.SizeDialog.currentColumns));
        for (int i = 0; i < GUI.SizeDialog.currentRows; ++i) {
            for (int j = 0; j < GUI.SizeDialog.currentColumns; ++j) {
               add(field.Cells[i][j]);   
            }
        }
    }

    public boolean isActive() {
        return activated;
    }

    public void setActive(boolean activated) {
        this.activated = activated;
    }
}

Field

package Model;

import java.awt.Dimension;
import java.util.ArrayList;

import GUI.SizeDialog;

public class Field {
    private int difficultyFactor;
    public static final int EASY = 1;
    public static final int NORMAL = 2;
    public static final int HARD = 3;
    public  Cell[][] Cells;

    public Field() {
        Cells = new Cell[GUI.SizeDialog.currentRows][GUI.SizeDialog.currentColumns];
        for (int i = 0; i < GUI.SizeDialog.currentRows; ++i) {
            for (int j = 0; j < GUI.SizeDialog.currentColumns; ++j) {
                Cells[i][j]= new Cell();

            }
        }

    }
    public void reset(){
        Cells = new Cell[GUI.SizeDialog.currentRows][GUI.SizeDialog.currentColumns];
        for (int i = 0; i < GUI.SizeDialog.currentRows; ++i) {
            for (int j = 0; j < GUI.SizeDialog.currentColumns; ++j) {
                Cells[i][j]= new Cell();
            }
        }
    }

    public void setDifficultyFactor(int level) {
        switch (level) {
        case EASY:
            difficultyFactor = EASY;
            break;
        case NORMAL:
            difficultyFactor = NORMAL;
            break;
        case HARD:
            difficultyFactor = HARD;
            break;
        default:
            difficultyFactor = NORMAL;
        }
    }
}

SizeDialog

package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Model.Field;


public class SizeDialog extends JDialog {

    private static final long serialVersionUID = 1L;
    private Model.Field field;
    private GUI.GameDisplay GameDisplay;
    public static Integer currentRows = 4;
    public static Integer currentColumns = 4;

    public SizeDialog() {
        JPanel dialogBoxPanel = new JPanel();
        BorderLayout dialogLayout = new BorderLayout();
        dialogBoxPanel.setLayout(dialogLayout);

        JSlider rowCount = new JSlider(4, 10, 4);
        JSlider columnCount = new JSlider(4, 10, 4);

        Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
        labelTable.put(new Integer(4), new JLabel("4"));
        labelTable.put(new Integer(10), new JLabel("10"));

        rowCount.setLabelTable(labelTable);
        rowCount.setPaintLabels(true);

        columnCount.setLabelTable(labelTable);
        columnCount.setPaintLabels(true);

        JLabel row = new JLabel();
        JLabel column = new JLabel();

        JPanel rowPanel = new JPanel();
        JPanel columnPanel = new JPanel();

        JLabel rowPosition = new JLabel();
        rowPosition.setText("Rows: 4");

        JLabel columnPosition = new JLabel();
        columnPosition.setText("Columns: 4");

        rowCount.addChangeListener(getCurrentRows(rowCount, "row", rowPosition));
        columnCount.addChangeListener(getCurrentColumns(columnCount, "column", columnPosition));

        JButton okButton = new JButton("OK");
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                field.reset();
                SizeDialog.this.dispose();

            }

        });
        rowPanel.add(rowPosition, BorderLayout.WEST);
        rowPanel.add(row, BorderLayout.CENTER);
        rowPanel.add(rowCount, BorderLayout.EAST);
        columnPanel.add(columnPosition, BorderLayout.WEST);
        columnPanel.add(column, BorderLayout.CENTER);
        columnPanel.add(columnCount, BorderLayout.EAST);

        dialogBoxPanel.add(rowPanel, BorderLayout.NORTH);
        dialogBoxPanel.add(columnPanel, BorderLayout.CENTER);
        dialogBoxPanel.add(okButton, BorderLayout.SOUTH);

        setContentPane(dialogBoxPanel);
        setMinimumSize(new Dimension(300, 150));
        setLocationRelativeTo(null);
    }



     private ChangeListener getCurrentColumns(final JSlider columnCount, final String string, final JLabel columnPosition) {
         ChangeListener changeListener = new ChangeListener() {
              public void stateChanged(ChangeEvent changeEvent) {
                JSlider theSlider = (JSlider) changeEvent.getSource();
                if (!theSlider.getValueIsAdjusting()) {
                    int sliderValue = theSlider.getValue();
                    columnPosition.setText("columns:" + sliderValue);    
                    SizeDialog.currentColumns = sliderValue;
                }
              }
         };
         return changeListener;
     }




    private ChangeListener getCurrentRows(final JSlider rowCount, final String string, final JLabel rowPosition) {
         ChangeListener changeListener = new ChangeListener() {
              public void stateChanged(ChangeEvent changeEvent) {
                JSlider theSlider = (JSlider) changeEvent.getSource();
                if (!theSlider.getValueIsAdjusting()) {
                    int sliderValue = theSlider.getValue();
                    rowPosition.setText("Rows:" + sliderValue);  
                    SizeDialog.currentRows = sliderValue;
                    }
                }
              };

        return changeListener;
      }
}

MainFrame

package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private static JPanel centerPanel;
    public static final String EASY = "Easy";
    public static final String NORMAL = "Normal";
    public static final String HARD = "Hard";

    public MainFrame(JPanel centerPanel) {
        // Setup of Main
        super("The True/False Game");
        this.centerPanel = centerPanel;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setup();
    }

    private void setup() {
        // Setup of centerPanel
        add(centerPanel);
        pack();
    }

    public JMenuBar setUpMenuBar(ActionListener listener) {
        // create the necessary menu items and menus
        JMenuBar menuBar = new JMenuBar();

        JMenu settingsMenu = new JMenu("Settings");
        JMenuItem size = new JMenuItem("Size");

        size.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object valueBox = new SizeDialog();
                ((Window) valueBox).setVisible(true);
            }
        });

        JMenu difficultyMenu = new JMenu("Difficulty Level");
        ButtonGroup levelGroup = new ButtonGroup();
        JCheckBoxMenuItem easyDifficultyItem = new JCheckBoxMenuItem(EASY);
        JCheckBoxMenuItem normalDifficultyItem = new JCheckBoxMenuItem(NORMAL);
        JCheckBoxMenuItem hardDifficultyItem = new JCheckBoxMenuItem(HARD);

        easyDifficultyItem.setSelected(true); // Default difficulty setting

        // this button group ensures that only one check box is selected
        levelGroup.add(easyDifficultyItem);
        levelGroup.add(normalDifficultyItem);
        levelGroup.add(hardDifficultyItem);

        difficultyMenu.add(easyDifficultyItem);
        difficultyMenu.add(normalDifficultyItem);
        difficultyMenu.add(hardDifficultyItem);

        settingsMenu.add(size);
        settingsMenu.add(difficultyMenu);
        menuBar.add(settingsMenu);

        // use a single listener to handle all menu item selections
        for (int i = 0; i < menuBar.getMenuCount(); ++i) {
            for (JMenuItem item : getMenuItems(menuBar.getMenu(i))) {
                item.addActionListener(listener);
            }
        }

        return menuBar;

    }

    // this recursion works because JMenu is a subclass of JMenuItem!
    private static List<JMenuItem> getMenuItems(JMenuItem item) {
        List<JMenuItem> items = new ArrayList<>();

        if (item instanceof JMenu) {
            JMenu menu = (JMenu) item;
            for (int i = 0; i < menu.getItemCount(); ++i) {
                items.addAll(getMenuItems(menu.getItem(i)));
            }
        } else {
            items.add(item);
        }

        return items;
    }

}

App

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuBar;

import Model.Cell;
import Model.Field;
import GUI.MainFrame;
import GUI.SizeDialog;

public class App {
    // Main Objects
    private static GUI.MainFrame MainFrame;
    private static GUI.GameDisplay GameDisplay;
    private static Model.Field field;
    public static GUI.SizeDialog SizeDialog;


    public static void main(String[] args) {
        // Setup of the Main Objects
        field = new Model.Field();
        GameDisplay = new GUI.GameDisplay(field);
        MainFrame = new MainFrame(GameDisplay);
        MainFrame.setMinimumSize(new Dimension(500, 500));

        MainFrame.getContentPane().add(GameDisplay);
        // Setup of the Menu Bar
        JMenuBar menuBar = MainFrame.setUpMenuBar(menuListener);
        MainFrame.setJMenuBar(menuBar);

        // Show the Mainframe in the center of the desktop screen
        MainFrame.setLocationRelativeTo(null);
        MainFrame.setVisible(true);
    }


    // listener for the menu items in MainFrame
    private static ActionListener menuListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            switch (e.getActionCommand()) {

            case GUI.MainFrame.EASY:
                field.setDifficultyFactor(Field.EASY);
                GameDisplay.setActive(true);
                break;

            case GUI.MainFrame.NORMAL:
                field.setDifficultyFactor(Field.NORMAL);
                GameDisplay.setActive(true);
                break;

            case GUI.MainFrame.HARD:
                field.setDifficultyFactor(Field.HARD);
                GameDisplay.setActive(true);
                break;
            }
        }

    };
};

1 个答案:

答案 0 :(得分:1)

目前正在Field班级中创建App班级的实例。您field类中的私有字段SizeDialog当前未设置为App类已知的实例。此问题的解决方案是将Field实例作为参数提供。

可能的解决方案如下:

应用:

public class App {
    // Main Objects
    private static GUI.MainFrame MainFrame;
    private static GUI.GameDisplay GameDisplay;
    private static Model.Field field;
    public static GUI.SizeDialog SizeDialog;


    public static void main(String[] args) {
        // Setup of the Main Objects
        field = new Model.Field();
        GameDisplay = new GUI.GameDisplay(field);
        MainFrame = new MainFrame(GameDisplay, field);

        /* the rest of your code */
    }

    /* more App code */
}

大型机:

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private static JPanel centerPanel;
    public static final String EASY = "Easy";
    public static final String NORMAL = "Normal";
    public static final String HARD = "Hard";

    private Model.Field field;

    public MainFrame(JPanel centerPanel, Model.Field field) {
        this.field = field;
        /* the rest of your code */
    }

    public JMenuBar setUpMenuBar(ActionListener listener) {
        // create the necessary menu items and menus
        JMenuBar menuBar = new JMenuBar();

        JMenu settingsMenu = new JMenu("Settings");
        JMenuItem size = new JMenuItem("Size");

        size.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object valueBox = new SizeDialog(field);
                ((Window) valueBox).setVisible(true);
            }
        });
        /* the rest of your code */
    }

    /* more MainFrame code */
}

SizeDialog:

public class SizeDialog extends JDialog {

    private static final long serialVersionUID = 1L;
    private final Model.Field field;
    private GUI.GameDisplay GameDisplay;
    public static Integer currentRows = 4;
    public static Integer currentColumns = 4;

    public SizeDialog(final Model.Field field) {
        this.field = field;
        /* the rest of your code */
    }

    /* more SizeDialog code */
}

编辑:如this answer中所述,匿名类中引用的对象必须是最终的。