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

Windows.java

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)
        frame.setSize(600,700);
//      Whether the window can be resized
        frame.setResizable(false);
//      Click to close
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      Set window layout
        frame.setLayout(new BorderLayout());
//      Display window
        frame.setVisible(true);
    }

    //  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);
        panel.add(jButton,c1);

//      Set transparency
        label1.setOpaque(true);
//      Set background color
        label1.setBackground(Color.white);
//      Set border color
        label1.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
//      label2 and label3 are the same
        label2.setOpaque(true);
        label2.setBackground(Color.white);
        label2.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
        label3.setOpaque(true);
        label3.setBackground(Color.white);
        label3.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
//      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);
        panel.add(label1,c2);
        GridBagConstraints c3=new GridBagConstraints(1,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
        panel.add(label2,c3);
        GridBagConstraints c4=new GridBagConstraints(2,1,1,1,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,new Insets(0,0,0,0),0,0);
        panel.add(label3,c4);
//      Add to top of layout
        frame.add(panel,BorderLayout.NORTH);
    }

//  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
        frame.add(container,BorderLayout.CENTER);
//      Create button
        for(int i=0;i<ROW;i++){
            for(int j=0;j<COL;j++){
                JButton btn=new JButton();
                btn.setOpaque(true);
                btn.setBackground(Color.yellow);
                container.add(btn);
                buttons[i][j]=btn;
            }
        }
    }

summary

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:

gridx=0,gridy=2,gridwidth=2,gridheight=1,weightx=0.2,weighty=0.5


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.

epilogue

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