Using java to realize mine sweeping games, no longer boring to learn grammar

Written at the beginning

The link to the previous blog is: Using java to realize mine sweeping games, no longer boring to learn grammar (1)

Task list

The list of tasks for bloggers to do this project is as follows:

Task 1: create window (completed)

Task 2: window layout

Task 3: define data structure

Task 4: realize the basic functions of the game (mine laying, mine checking, demining, judging the win or loss)

Task 5: auxiliary functions (to be opened, opened, game reopened, timer)

code implementation

package game;

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

import static game.Game.*;

public class Windows{

//  Create public static objects for easy calling
    public static JFrame frame=new JFrame();
    public static JButton jButton=new JButton();
    public static JLabel label1=new JLabel("Stay open:"+UNOPEN);
    public static JLabel label2=new JLabel("Already opened:"+OPENED);
    public static JLabel label3=new JLabel("Use time:"+second+"s");

//  create a window
    public void createWindows(){
//      Define window size (600 * 700)
//      Whether the window can be resized
//      Click to close
//      Set window layout
        frame.setLayout(new BorderLayout());
//      Display window

    //  Create button at top of window
    public void setHeader(){
//      Create layout
        JPanel panel=new JPanel(new GridBagLayout());
        GridBagConstraints c1=new GridBagConstraints(0,0,3,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);

//      Set transparency
//      Set background color
//      Set border color
//      label2 and label3 are the same
//      Set three status display bars
        GridBagConstraints c2=new GridBagConstraints(0,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
        GridBagConstraints c3=new GridBagConstraints(1,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
        GridBagConstraints c4=new GridBagConstraints(2,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
//      Add to top of layout

//  Setup button
    public void setButtons(){
//      Create container
        Container container=new Container();
//      Create layout
        container.setLayout(new GridLayout(ROW,COL));
//      Add to center of overall layout
//      Create button
        for(int i=0;i<ROW;i++){
            for(int j=0;j<COL;j++){
                JButton btn=new JButton();


This part mainly uses swing component and awt component to layout the interface. The two layouts used in the tutorial are GridBagLayout and GridLayout.

GridBagLayout design concept is to regard the parent container as a table, the length and width of cells in the table can be adjusted according to the needs, and the cell size can be adjusted by the position of vertical and horizontal lines, instead of setting the cell size arbitrarily. Components can be placed in one or more consecutive cells, and the last area formed by these consecutive cells must be a square area .

For example, if we want to complete such a layout, as shown in the figure below, what should we do?

Let's take a look at the parameter properties of GridBagConstraints in GridBagLayout

Variable name Effect
gridx The row number of the cell in the upper left corner of the grid. The row number starts with 0 in the table and is numbered from left to right
gridy The column number of the cell in the upper left corner of the grid. The column number starts with 0 in the table and is numbered from top to bottom
gridwidth Number of cells occupied by the grid in the horizontal direction
gridheight Number of cells occupied by the grid in the vertical direction
weightx The ratio of the length of the grid to the length of the table (that is, the length of the parent container). If it is 0, it is the default length of the component
weighty The ratio of the width of the grid to the width of the table (that is, the width of the parent container). If it is 0, it is the default width of the component

As shown in the figure below, the parent container is divided into 3 * 3 tables, and then grid 1, grid 2 and grid 3 are placed in the container. Take grid 3 as an example, the parameters are:


What kind of layout is GridLayout?

GridLayout is a traditional grid layout. You only need to define a few lines and columns and then add components in turn. It's very convenient.


If this article allows you to learn a certain amount of knowledge, you might as well point a praise and attention, so that bloggers can see. If there are any mistakes and omissions in the explanation, please point out or ask questions in the comments. The blogger will update and reply as soon as possible. Thank you!

Published 6 original articles, won praise 9, visited 100
Private letter follow

Tags: Java Windows

Posted on Tue, 14 Jan 2020 21:12:31 -0800 by enoyhs