java—创建一个网格布局,其中包含100个按钮,80个空按钮和20个随机按钮

bmvo0sr5  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(320)

我们正在课堂上做一个寻宝游戏作为一个项目。下面是我们到目前为止的代码。我需要随机化宝藏按钮的位置。我的问题是:我是否需要创建一个数组来保存emptybutton和treasurebutton,然后对其调用random,还是有办法在treasurebutton类中创建一个随机布局?
这是我们目前的代码

public class TreasureGame
{
    // has-a number of tries
    private int numberOfTriesLeft = 50;
    //has-a number of hidden treasure
    private int numberStillHidden = 20;
    // Purpose: Subtract 1 try for every attempt taken by the player
    public void reduceTriesLeft()
    {
        numberOfTriesLeft -= 1;
    }

    public void reduceNumberStillHidden()
    {
        numberStillHidden -=1;
    }

    public int getNumberStillHidden()
    {
        return numberStillHidden;
    }

    public int getNumberOfTriesLeft()
    {
        return numberOfTriesLeft;
    }
}
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class TreasureGameView extends JFrame
{
    // has-a TreasureGame
    private TreasureGame treasureGame;
    //has-a triesLeftField
    private JTextField triesLeftField;
    //has-a treasures left field
    private JTextField treasuresLeftField;
    private JTextField lastMoveField;

    public TreasureGameView(TreasureGame newTreasureGame)
    {
        treasureGame = newTreasureGame;
        setTitle("Treasure Hunt");
        setSize(800, 500);
        setLayout(new BorderLayout());

        JPanel gridPanel = new JPanel();
        gridPanel.setLayout(new GridLayout(10, 10));
        add(gridPanel, BorderLayout.CENTER);

        for(int counter=0; counter<treasureGame.getNumberStillHidden(); counter++)
        {

            gridPanel.add(new TreasureButton(treasureGame, this));

        }
        for(int counter=0; counter<(10*10)-treasureGame.getNumberStillHidden(); counter++)
        {

            gridPanel.add(new EmptyButton(treasureGame, this));
        }

        JPanel triesLeftPanel = new JPanel();

        JLabel triesLeftLabel = new JLabel("Tries left:");
        triesLeftPanel.add(triesLeftLabel);
        triesLeftField = new JTextField("" + treasureGame.getNumberOfTriesLeft());
        triesLeftPanel.add(triesLeftField);
        JLabel treasuresLeftLabel = new JLabel("Treasures left:");
        triesLeftPanel.add(treasuresLeftLabel);
        treasuresLeftField = new JTextField("" + treasureGame.getNumberStillHidden());
        triesLeftPanel.add(treasuresLeftField);

        add(triesLeftPanel, BorderLayout.WEST);

        JPanel lastMovePanel = new JPanel();
        JLabel lastMoveLabel = new JLabel("Your last move was: ");
        lastMovePanel.add(lastMoveLabel);
        lastMoveField = new JTextField("" );
        lastMovePanel.add(lastMoveField);
        add(lastMovePanel, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    //Purpose; Update Value of triesLeftField
    public void updateUI()
    {
        triesLeftField.setText("" + treasureGame.getNumberOfTriesLeft());
        treasuresLeftField.setText("" + treasureGame.getNumberStillHidden());
    }
}
import javax.swing.*;
public class EmptyButton extends JButton
{
    //has-a TreasureGame
    private TreasureGame game;

    //has-a TreasureGameView
    private TreasureGameView gameView;
    public EmptyButton(TreasureGame game, TreasureGameView gameView)
    {
        super();
        this.game = game;
        this.gameView = gameView;
        //setText("?");
        addActionListener(new EmptyButtonListener(this, game, gameView));
    }
}
public class TreasureButton extends EmptyButton
{
   public TreasureButton(TreasureGame game, TreasureGameView gameView)
   {
       super(game, gameView);

       //setText("T"); 
       addActionListener(new TreasureButtonListener(this, game, gameView));
   }

}
import java.awt.event.*;
public class EmptyButtonListener implements ActionListener
{
    private EmptyButton button;
    //has-a TreasureGame
    private TreasureGame  game;
    //has-a TreasureGameView
    private TreasureGameView gameView;

    public EmptyButtonListener(EmptyButton button, TreasureGame game, TreasureGameView gameView)
    {
        this.game = game;
        this.button = button;
        this.gameView = gameView;
    }

    public void actionPerformed(ActionEvent e)
    {
        //button.setText("X");
        button.setEnabled(false);
        game.reduceTriesLeft();
        gameView.updateUI();
    }
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TreasureButtonListener implements ActionListener
{
    // Has-a TreasureButton
    private TreasureButton button;
    private TreasureGame game;
    private TreasureGameView gameView;

    public TreasureButtonListener(TreasureButton button, TreasureGame game, TreasureGameView gameView)
    {
        this.button = button;
        this.game = game;
        this.gameView = gameView;
    }

    public void actionPerformed(ActionEvent e)
    {
        button.setText("$");
        game.reduceNumberStillHidden();
        gameView.updateUI();
    }
}
ergxz8rk

ergxz8rk1#

你必须用mvc来思考。
首先为你的宝藏建立一个模型。这可能是类字段的数组。
一个领域将代表它是否拥有一个宝藏和一个状态
用20代表宝藏的字段填充数组。
洗牌数组。
然后,视图将遍历数组,并以用户友好的方式表示模型。为了方便起见,您可以将字段示例传递给按钮,以操纵actionlistener中的字段状态。

rnmwe5a2

rnmwe5a22#

答案主要是基于意见。最好保持一个 List<Buttons> buttons 并储存两者 empty 以及 TreasureButton 在里面。当你需要随机化时,使用 Collections.shuffle(buttons) 洗牌。由于操作保持不变,您不需要每次都创建按钮。只需洗牌它和显示在网格中的迭代列表。

相关问题