如何让程序重新生成新游戏,其中以前的网格字母与新的网格字母互换

How to have the program regenerate a quot;new gamequot; where previous grid letters get swapped with new grid letters(如何让程序重新生成新游戏,其中以前的网格字母与新的网格字母互换)

本文介绍了如何让程序重新生成新游戏,其中以前的网格字母与新的网格字母互换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个程序中,我试图输入一个功能来开始游戏,如果我点击开始游戏(MainMenu),一个新的jpanel就会打开(MainGame),创建另一个在网格中创建jButton的jpanel。如果我返回并再次单击开始游戏,应该会生成一个新的网格,而不是以前的网格,从而有效地开始一个新的游戏。问题是,如果我返回并再次点击新游戏,程序会创建两个网格。

我已尝试使用=NULL删除网格面板的实例,但不起作用

主要功能:

import javax.swing.*;
import java.awt.*;
import java.awt.CardLayout;

public class Game extends JFrame {
    MainMenu mainMenu;
    Settings settings;
    MainGame mainGame;
    CardLayout cl;
    JPanel container;

    public Game(){
        setSize(900,900); //have all as seperate classes
        setDefaultCloseOperation(3); //cl call container
        container  = new JPanel(); //container call menu1 and menu2
        cl = new CardLayout();
        mainMenu = new MainMenu();
        settings = new Settings();
        mainGame = new MainGame();
        mainMenu.setSettings(settings);
        settings.setMainMenu(mainMenu);
        settings.setMainGame(mainGame);
        mainMenu.setMainGame(mainGame);
        mainGame.setMainMenu(mainMenu);
        mainGame.setSettings(settings);
        container.setLayout(cl); //this stays here i think



        //add setter for main game here
        container.add(mainMenu,"1");
        container.add(settings,"2");
        container.add(mainGame,"3");
        mainMenu.setContainer(container);
        mainMenu.setCl(cl);
        settings.setContainer(container);
        settings.setCl(cl);
        mainGame.setContainer(container);
        mainGame.setCl(cl);
        cl.show(container, "1");
        add(container,BorderLayout.CENTER);

    }

    public static void main(String[] args) {
        Game game = new Game();
        game.setVisible(true);
    }
}

主游戏类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MainGame extends JPanel {
    MainMenu mainMenu;
    Settings settings;
    CardLayout cl;
    JPanel container;
    String rows;
    String columns;

    public void setMainMenu(MainMenu mainMenu) {
        this.mainMenu = mainMenu;
    }

    public void setSettings(Settings settings) {
        this.settings = settings;
    }

    public void setCl(CardLayout cl) {
        this.cl = cl;
    }

    public void setContainer(JPanel container) {
        this.container = container;
    }

    public void setRows(String rows) {
        this.rows = rows;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }
    public MainGame(){
        JPanel north = new JPanel();
        north.setLayout(new FlowLayout());
        ReturnAction returnAl = new ReturnAction();
        JButton Return2 = new JButton("Return");
        Return2.addActionListener(returnAl);
        north.add(Return2);
        add(north);
    }
    class ReturnAction implements ActionListener {
        @Override

        public void actionPerformed(ActionEvent e) {
            cl.show(container,"1");
        }
    }
}

主菜单类(这个包含游戏生成部分):

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MainMenu extends JPanel {
JPanel grid = new JPanel();
MainGame mainGame;
Settings settings;
CardLayout cl;
JPanel container;
String rows;
String columns;
boolean checkexists = false;
int rownumber;
int columnnumber;


public void setMainGame(MainGame mainGame) {
    this.mainGame = mainGame;
}

public void setCl(CardLayout cl) {
    this.cl = cl;
}

public void setContainer(JPanel container) {
    this.container = container;
}

public void setSettings(Settings settings) {
    this.settings = settings;
}

public void setRows(String rows) {
    this.rows = rows;
}

public void setColumns(String columns) {
    this.columns = columns;
}

public MainMenu() {
    setLayout(new GridLayout(3, 1));
    JButton Newgame = new JButton("New Game");
    JButton Cont = new JButton("Continue");
    JButton Sett = new JButton("Settings");
    add(Newgame);
    add(Cont);
    SwitchMenu1 switchMenu1 = new SwitchMenu1();
    SwitchMenu2 switchMenu2 = new SwitchMenu2();
    GenerateGame generateGame = new GenerateGame();
    Newgame.addActionListener(switchMenu2);
    Newgame.addActionListener(generateGame);
    Sett.addActionListener(switchMenu1);
    add(Sett);

}

class SwitchMenu1 implements ActionListener {
    @Override

    public void actionPerformed(ActionEvent e) {
        cl.show(container, "2");

    }
}

class SwitchMenu2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        cl.show(container, "3");
    }
}

class GenerateGame implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (checkexists == true){
            grid = null;
            grid = new JPanel();
        }
        try {
            rownumber = Integer.parseInt(rows);
        } catch (NumberFormatException be) {
            rownumber = 10;
        }
        try {
            columnnumber = Integer.parseInt(columns);
        } catch (NumberFormatException be) {
            columnnumber = 10;
        }
        Random rand = new Random();
        int randomnumber;
        String Letters = "AAIIOOUUEEABCDEFGHIJKLMNOPQRSTUVWXYZ";
        grid.setLayout(new GridLayout(rownumber, columnnumber));
        JButton[][] gridbutton = new JButton[rownumber][columnnumber];
        MainbuttonAL mainbuttonAL = new MainbuttonAL();
        for (int i = 0; i < rownumber; i++) {
            for (int j = 0; j < columnnumber; j++) {
                if (checkexists == true){
                    gridbutton[i][j] = null;
                }
                randomnumber = rand.nextInt(Letters.length());
                gridbutton[i][j] = new JButton("" + Letters.charAt(randomnumber));
                gridbutton[i][j].addActionListener(mainbuttonAL);
                grid.add(gridbutton[i][j]);
            }
        }
        mainGame.add(grid);
        checkexists = true;
    }
}

class MainbuttonAL implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {

    }
}
}

设置类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class MainMenu extends JPanel {
    JPanel grid = new JPanel();
    MainGame mainGame;
    Settings settings;
    CardLayout cl;
    JPanel container;
    String rows;
    String columns;
    boolean checkexists = false;
    int rownumber;
    int columnnumber;


    public void setMainGame(MainGame mainGame) {
        this.mainGame = mainGame;
    }

    public void setCl(CardLayout cl) {
        this.cl = cl;
    }

    public void setContainer(JPanel container) {
        this.container = container;
    }

    public void setSettings(Settings settings) {
        this.settings = settings;
    }

    public void setRows(String rows) {
        this.rows = rows;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }

    public MainMenu() {
        setLayout(new GridLayout(3, 1));
        JButton Newgame = new JButton("New Game");
        JButton Cont = new JButton("Continue");
        JButton Sett = new JButton("Settings");
        add(Newgame);
        add(Cont);
        SwitchMenu1 switchMenu1 = new SwitchMenu1();
        SwitchMenu2 switchMenu2 = new SwitchMenu2();
        GenerateGame generateGame = new GenerateGame();
        Newgame.addActionListener(switchMenu2);
        Newgame.addActionListener(generateGame);
        Sett.addActionListener(switchMenu1);
        add(Sett);

    }

    class SwitchMenu1 implements ActionListener {
        @Override

        public void actionPerformed(ActionEvent e) {
            cl.show(container, "2");

        }
    }

    class SwitchMenu2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            cl.show(container, "3");
        }
    }

    class GenerateGame implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (checkexists == true){
                grid = null;
                grid = new JPanel();
            }
            try {
                rownumber = Integer.parseInt(rows);
            } catch (NumberFormatException be) {
                rownumber = 10;
            }
            try {
                columnnumber = Integer.parseInt(columns);
            } catch (NumberFormatException be) {
                columnnumber = 10;
            }
            Random rand = new Random();
            int randomnumber;
            String Letters = "AAIIOOUUEEABCDEFGHIJKLMNOPQRSTUVWXYZ";
            grid.setLayout(new GridLayout(rownumber, columnnumber));
            JButton[][] gridbutton = new JButton[rownumber][columnnumber];
            MainbuttonAL mainbuttonAL = new MainbuttonAL();
            for (int i = 0; i < rownumber; i++) {
                for (int j = 0; j < columnnumber; j++) {
                    if (checkexists == true){
                        gridbutton[i][j] = null;
                    }
                    randomnumber = rand.nextInt(Letters.length());
                    gridbutton[i][j] = new JButton("" + Letters.charAt(randomnumber));
                    gridbutton[i][j].addActionListener(mainbuttonAL);
                    grid.add(gridbutton[i][j]);
                }
            }
            mainGame.add(grid);
            checkexists = true;
        }
    }

    class MainbuttonAL implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

        }
    }
}

我可以使用什么方法重新生成网格?

推荐答案

一般来说,您应该致力于分离视图和数据的概念,这意味着您可以将游戏模型应用于视图,然后该视图将根据模型属性进行自我修改,这通常称为";模型-视图-控制器。

问题是,在创建新游戏时,您从不从其父容器中删除grid

if (checkexists == true){
    grid = null;
    grid = new JPanel();
}

相反,在重新初始化grid之前,您应该从其父容器中删除

if (grid != null) {
    mainGame.remove(grid);
    grid = null;
    grid = new JPanel();
}

或者您可以只从grid面板本身中删除组件

grid.removeAll();

另一种方法...

在所有阶段,您都应该尝试将对象和工作流彼此分离,以便更轻松地更改任何一个部分,而不会对系统或工作流的其他部分造成不利影响。

例如,查看您的代码,导航决策与每个面板/视图紧密耦合,但在现实中,他们不应该知道或关心导航如何工作(或存在其他视图),他们应该只做这些工作。

您可以通过使用委托(由观察者支持)来分离此工作流。这基本上意味着,单独的视图并不关心导航的工作方式,只有当它请求执行某些操作时,导航才会发生。

您应该花时间通读...

  • Model-View-Controller
  • Observer Pattern
  • Dependency Injection in Java

但这对您有什么帮助?嗯,它会一直帮助你的!

让我们从游戏本身开始。我们首先需要某种容器来保存游戏的数据库逻辑,因此根据您当前的代码,它可能类似于...

public interface GameModel {
    public int getRows();
    public int getColumns();
}

我知道,很神奇,不是吗?但这个interface会增长到包含运行游戏所需的逻辑。

现在,我们可以将其应用于GamePanel

public class GamePane extends JPanel {
    
    public interface Obsever {
        public void back(GamePane source);
    }

    private GameModel model;
    private Obsever obsever;
    
    private JPanel contentPane;
    
    private ActionListener buttonActionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            didTap(e.getActionCommand());
        }
    };
    
    public GamePane(Obsever obsever) {
        this.obsever = obsever;
        
        setLayout(new BorderLayout());
        
        contentPane = new JPanel();
        add(new JScrollPane(contentPane));
        
        JButton backButton = new JButton("<< Back");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                obsever.back(GamePane.this);
            }
        });
        
        JPanel bannerPane = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.anchor = GridBagConstraints.LINE_END;
        
        bannerPane.add(backButton, gbc);
        
        add(bannerPane, BorderLayout.NORTH);
    }

    @Override
    public Dimension getPreferredSize() {
        // Bunch of things we could do here, but this basically
        // acts as a stand in for CardLayout, otherwise the primary
        // view will be to small
        return new Dimension(800, 800);
    }

    public void setModel(GameModel model) {
        if (this.model == model) {
            // Do nothing, nothings changed
            return;
        }
        this.model = model;
        buildUI();
    }

    protected void buildUI() {
        contentPane.removeAll();
        if (model == null) {
            return;
        }
        String Letters = "AAIIOOUUEEABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random rnd = new Random();
        JButton[][] gridbutton = new JButton[model.getRows()][model.getColumns()];
        contentPane.setLayout(new GridLayout(model.getRows(), model.getColumns()));
        //Game.MainMenu.MainbuttonAL mainbuttonAL = new Game.MainMenu.MainbuttonAL();
        for (int i = 0; i < model.getRows(); i++) {
            for (int j = 0; j < model.getColumns(); j++) {
                int randomnumber = rnd.nextInt(Letters.length());
                gridbutton[i][j] = new JButton("" + Letters.charAt(randomnumber));
                //gridbutton[i][j].addActionListener(mainbuttonAL);
                contentPane.add(gridbutton[i][j]);
            }
        }
    }
    
    protected void didTap(String action) {
        
    }

}
现在,好的多汁部分在模型更改时由setModel调用的buildUI中。这只是基于GameModel属性重新生成用户界面。

关于导航概念,您可以通过ObserverinterfaceGamePane中看到它的一部分。我首先创建了一个单独的类来处理导航工作流。

这意味着";如何实现";或";实现详细信息与系统的其他部分分离或隐藏。相反,它使用简单的观察者/委派工作流。

每个视图都提供一个Observer(更好的名称)来描述它需要执行的导航操作。例如,SettingsPaneGamePane都只有back。他们不在乎他们之前发生了什么,这是导航控制器的决定。

public class NavigationPane extends JPanel {

    enum View {
        MAIN_MENU, GAME, SETTINGS
    }

    private CardLayout cardLayout;
    private GameModel model;
    private GamePane gamePane;
    
    // Just for testing...
    private Random rnd = new Random();

    public NavigationPane() {
        cardLayout = new CardLayout();
        setLayout(cardLayout);

        add(new MainMenu(new MainMenu.Observer() {
            @Override
            public void newGame(MainMenu source) {
                gamePane.setModel(createModel());
                navigateTo(View.GAME);
            }

            @Override
            public void continueGame(MainMenu source) {
                // Because it's possible to push continue
                // without starting a game
                // It might be possible have a "menu" model
                // which can be used to change the enabled state of
                // the continue button based on the state of the
                // game
                gamePane.setModel(getOrCreateGameModel());
                navigateTo(View.GAME);
            }

            @Override
            public void settingsGame(MainMenu source) {
                navigateTo(View.SETTINGS);
            }
        }), View.MAIN_MENU);
        
        gamePane = new GamePane(new GamePane.Obsever() {
            @Override
            public void back(GamePane source) {
                navigateTo(View.MAIN_MENU);
            }
        });
        add(gamePane, View.GAME);
        
        add(new SettingsPane(new SettingsPane.Obsever() {
            @Override
            public void back(SettingsPane source) {
                navigateTo(View.MAIN_MENU);
            }
        }), View.SETTINGS);

        navigateTo(View.MAIN_MENU);
    }
    
    protected GameModel createModel() {
        model = new DefaultGameModel(rnd.nextInt(9) + 2, rnd.nextInt(9) + 2);
        return model;
    }
    
    protected GameModel getOrCreateGameModel() {
        if (model == null) {
            model = createModel();
        }
        return model;
    }

    protected void add(Component component, View view) {
        add(component, view.name());
    }

    protected void navigateTo(View view) {
        cardLayout.show(this, view.name());
    }

}

可运行示例...

所以,这是很多脱离上下文的代码。以下基本上是您可以采取的一种可能方法的示例,以进一步减少目前代码库中正在增长的一些杂乱和耦合。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new NavigationPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class NavigationPane extends JPanel {

        enum View {
            MAIN_MENU, GAME, SETTINGS
        }

        private CardLayout cardLayout;
        private GameModel model;
        private GamePane gamePane;

        // Just for testing...
        private Random rnd = new Random();

        public NavigationPane() {
            cardLayout = new CardLayout();
            setLayout(cardLayout);

            add(new MainMenu(new MainMenu.Observer() {
                @Override
                public void newGame(MainMenu source) {
                    gamePane.setModel(createModel());
                    navigateTo(View.GAME);
                }

                @Override
                public void continueGame(MainMenu source) {
                    // Because it's possible to push continue
                    // without starting a game
                    // It might be possible have a "menu" model
                    // which can be used to change the enabled state of
                    // the continue button based on the state of the
                    // game
                    gamePane.setModel(getOrCreateGameModel());
                    navigateTo(View.GAME);
                }

                @Override
                public void settingsGame(MainMenu source) {
                    navigateTo(View.SETTINGS);
                }
            }), View.MAIN_MENU);

            gamePane = new GamePane(new GamePane.Obsever() {
                @Override
                public void back(GamePane source) {
                    navigateTo(View.MAIN_MENU);
                }
            });
            add(gamePane, View.GAME);

            add(new SettingsPane(new SettingsPane.Obsever() {
                @Override
                public void back(SettingsPane source) {
                    navigateTo(View.MAIN_MENU);
                }
            }), View.SETTINGS);

            navigateTo(View.MAIN_MENU);
        }

        protected GameModel createModel() {
            model = new DefaultGameModel(rnd.nextInt(9) + 2, rnd.nextInt(9) + 2);
            return model;
        }

        protected GameModel getOrCreateGameModel() {
            if (model == null) {
                model = createModel();
            }
            return model;
        }

        protected void add(Component component, View view) {
            add(component, view.name());
        }

        protected void navigateTo(View view) {
            cardLayout.show(this, view.name());
        }

    }

    public class MainMenu extends JPanel {

        public interface Observer {
            public void newGame(MainMenu source);
            public void continueGame(MainMenu source);
            public void settingsGame(MainMenu source);
        }

        private Observer observer;

        public MainMenu(Observer observer) {
            this.observer = observer;
            setLayout(new GridBagLayout());

            JButton newGameButton = new JButton("New Game");
            JButton continueButton = new JButton("Continue");
            JButton settingsButton = new JButton("Settings");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(newGameButton, gbc);
            add(continueButton, gbc);
            add(settingsButton, gbc);

            newGameButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.newGame(MainMenu.this);
                }
            });

            continueButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.continueGame(MainMenu.this);
                }
            });

            settingsButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    observer.settingsGame(MainMenu.this);
                }
            });
        }

    }

    public class SettingsPane extends JPanel {

        public interface Obsever {
            public void back(SettingsPane source);
        }

        public SettingsPane(Obsever obsever) {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JLabel("All your setting belong to us"), gbc);

            JButton backButton = new JButton("<< Back");
            backButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    obsever.back(SettingsPane.this);
                }
            });

            add(backButton, gbc);
        }

    }

    public interface GameModel {
        public int getRows();
        public int getColumns();
    }

    public class DefaultGameModel implements GameModel {

        private int rows;
        private int columns;

        public DefaultGameModel(int rows, int columns) {
            this.rows = rows;
            this.columns = columns;
        }

        @Override
        public int getRows() {
            return rows;
        }

        @Override
        public int getColumns() {
            return columns;
        }

    }

    public class GamePane extends JPanel {

        public interface Obsever {
            public void back(GamePane source);
        }

        private GameModel model;
        private Obsever obsever;

        private JPanel contentPane;

        private ActionListener buttonActionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                didTap(e.getActionCommand());
            }
        };

        public GamePane(Obsever obsever) {
            this.obsever = obsever;

            setLayout(new BorderLayout());

            contentPane = new JPanel();
            add(new JScrollPane(contentPane));

            JButton backButton = new JButton("<< Back");
            backButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    obsever.back(GamePane.this);
                }
            });

            JPanel bannerPane = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.weightx = 1;
            gbc.anchor = GridBagConstraints.LINE_END;

            bannerPane.add(backButton, gbc);

            add(bannerPane, BorderLayout.NORTH);
        }

        @Override
        public Dimension getPreferredSize() {
            // Bunch of things we could do here, but this basically
            // acts as a stand in for CardLayout, otherwise the primary
            // view will be to small
            return new Dimension(800, 800);
        }

        public void setModel(GameModel model) {
            if (this.model == model) {
                // Do nothing, nothings changed
                return;
            }
            this.model = model;
            buildUI();
        }

        protected void buildUI() {
            contentPane.removeAll();
            if (model == null) {
                return;
            }
            String Letters = "AAIIOOUUEEABCDEFGHIJKLMNOPQRSTUVWXYZ";
            Random rnd = new Random();
            JButton[][] gridbutton = new JButton[model.getRows()][model.getColumns()];
            contentPane.setLayout(new GridLayout(model.getRows(), model.getColumns()));
            //Game.MainMenu.MainbuttonAL mainbuttonAL = new Game.MainMenu.MainbuttonAL();
            for (int i = 0; i < model.getRows(); i++) {
                for (int j = 0; j < model.getColumns(); j++) {
                    int randomnumber = rnd.nextInt(Letters.length());
                    gridbutton[i][j] = new JButton("" + Letters.charAt(randomnumber));
                    //gridbutton[i][j].addActionListener(mainbuttonAL);
                    contentPane.add(gridbutton[i][j]);
                }
            }
        }

        protected void didTap(String action) {

        }

    }
}

这篇关于如何让程序重新生成新游戏,其中以前的网格字母与新的网格字母互换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何让程序重新生成新游戏,其中以前的网格字母与新的网格字母互换

基础教程推荐