繁体   English   中英

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

[英]How to reset Minesweeper game board in Java.Swing

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

PS重置必须在“类别”字段中。

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

    };
};

目前正在App类中创建Field类的实例。 您的私人场fieldSizeDialog类目前没有被设置为已知实例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 */
}

大小对话框:

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 */
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM