How to Create a GUI Grid in Java (with Images)

Table of contents:

How to Create a GUI Grid in Java (with Images)
How to Create a GUI Grid in Java (with Images)
Anonim

The Grid doesn't do anything special at this point, but with a little research, you can add some actionlisteners and some logic to make a simple 2D game like tic-tac-toe, or more complicated like Battleship.

Note: This article uses Eclipse for all examples, so things may be different depending on your IDE. It should be very similar to what you'll need in JCreator, but it's almost useless for a GUI-based IDE, like NetBeans mainly for NetBeans' drag and drop method.

Steps

Make a GUI Grid in Java Step 1
Make a GUI Grid in Java Step 1

Step 1. Create a Java project

This is quite simple. Open your IDE and create a new project. Call it what you want. In the example it will be buttongrid.

  • This name doesn't really matter as it is just the name that will be given to the file.

    Make a GUI Grid in Java Step 2
    Make a GUI Grid in Java Step 2

    Step 2. Create a new Java class with a constructor

    Create a new class and name it as you wish. In this example it will be buttongrid. For an Eclipse user you will need to turn on the check called public static void main (string args), so you don't have to type it when you start.

    • This name is more important than the previous one as it must be a single word or else it will be unusable.

      Make a GUI Grid in Java Step 3
      Make a GUI Grid in Java Step 3

      Step 3. Import the libraries

      These contain all the information you will need to write the code presented here. You will need to import javax.swing. JFrame, javax.swing. JButton, and java.awt. Gridlayout. These are placed before the start of the class, between lines 1 and 3, the order in which they are listed is not important.

      Make a GUI Grid in Java Step 4
      Make a GUI Grid in Java Step 4

      Step 4. Create a constructor

      The constructor creates a new instance of the buttongrid class allowing the different buttongrids to have separate information. All constructors must be called the same way as the class. The constructor doesn't need anything first, but 'public' is often entered for ease of reference. Constructors are often placed as the first method in a class, so right after the class name, however it must be placed inside the class. The buttongrid constructor needs parameters, which are placed inside brackets after the constructor name. In this example the parameters are two integers 'x' and 'y'.

      Make a GUI Grid in Java Step 5
      Make a GUI Grid in Java Step 5

      Step 5. Create a Frame:

      1. The frame must be declared. To make sure it can be referenced outside the ButtonGrid constructor, place it outside that method, but inside the class. Most variables are declared at the beginning of the class, right above the constructor. To create a new frame, type: JFrame frame = new JFrame ();
      2. Within the constructor we need to make sure that all buttons are placed within the grid layout. To do this we set the frame layout, writing: frame.setLayout (new GridLayout (x, y));

      3. Not necessarily mandatory, but to make the frame close by pressing the 'x' in the upper right corner, we need to add the line: frame.setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
      4. In order for the frame to be the correct size for everything to fit in, we need to run the pack command: frame.pack ();

      5. Lastly, we need to make the frame visible: frame.setVisible (true);

        Make a GUI Grid in Java Step 6
        Make a GUI Grid in Java Step 6

        Step 6. Create the button grid:

        1. The buttons that users interact with need to be created, but since we don't know how many we need, they need to be declared first. So just below the frame creation line, we create the buttons: JButton grid; The two groups of square brackets are used to indicate that the JButtons are inserted in a two-dimensional format within the grid. If there was only one set of square brackets, there would only be one JButton line, which still works, it's just easier to create or interact with them if it's two dimensional.
        2. The JButtons have been declared, but we should always say how many buttons there are. You need to add a line of code in the constructor to set the quantity: grid = new JButton [width] [length];

        3. Now that it has been decided that there will be a certain number of buttons, one will need to be created at a time. The easiest way to do this is with two loops, one for the x-axis, one for the y-axis. Inside the two loops we create a new button, and for simplicity of reference the example inserts some text inside all the buttons in order to understand which button within the two-dimensional array is where. To create a button, inside the loop you have to put grid [x] [y] = new JButton ("(" + x + "," + y + ")");

          Make a GUI Grid in Java Step 7
          Make a GUI Grid in Java Step 7

          Step 7. Add the buttons to the window

          Inside the loop we need to insert the buttons inside the frame with a simple command: frame.add (grid [x] [y]);

          Make a GUI Grid in Java Step 8
          Make a GUI Grid in Java Step 8

          Step 8. Create a ButtonGrid Instance

          In your main class, type: new ButtonGrid (3, 3); The two groups of threes create a 3 by 3 grid, and any positive numbers can be inserted into it.

          Make a GUI Grid in Java Step 9
          Make a GUI Grid in Java Step 9

          Step 9. Run the program

          To do this in Eclipse press Ctrl + F11

          Make a GUI Grid in Java Step 10
          Make a GUI Grid in Java Step 10

          Step 10. Find out more about java:

          java.sun.com/j2se/1.4.2/docs/api/index-files/index-1.html

          Additional things with buttons: To make the buttons do something look at the actionListener ()

          Method 1 of 1: Code Step

          The main class:

          public class ButtonGrid {public static void main (String args) {}}

          Imports:

          import javax.swing. JFrame; import javax.swing. JButton; import java.awt. GridLayout; public class ButtonGrid {…

          Constructor Code:

          public class ButtonGrid {public ButtonGrid (int width, int length) {}}…

          Frame Code:

          public class ButtonGrid {JFrame frame = new Jframe (); public ButtonGrid (int width, int length) {frame.setLayout (new GridLayout (width, length)); frame.setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE); frame.pack (); frame.setVisible (true); }}…

          Button grid code:

          | JFrame frame = new JFrame (); // creates frame JButton grid; // names the grid of buttons public ButtonGrid (int width, int length) {// constructor with 2 parameters frame.setLayout (new GridLayout (width, length)); // set layout of frame grid = new JButton [width] [length]; // allocate the size of grid for (int y = 0; y <length; y ++) {for (int x = 0; x <width; x ++) {grid [x] [y] = new JButton ("(" + x + "," + y + ")"); frame.add (grid [x] [y]); // adds button to grid}} frame.setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE); frame.pack (); frame.setVisible (true); }…

          Add Buttons to Frame:

          for (int y = 0; y <length; y ++) {for (int x = 0; x <width; x ++) {grid [x] [y] = new JButton ("(" + x + "," + y + ") "); frame.add (grid [x] [y]); }}…

          Create a ButtonGrid instance:

          public static void main (String args) {new ButtonGrid (3, 3); // makes new ButtonGrid with 2 parameters}…

          Final Code:

          import javax.swing. JFrame; // imports JFrame library import javax.swing. JButton; // imports JButton library import java.awt. GridLayout; // imports GridLayout library public class ButtonGrid {JFrame frame = new JFrame (); // creates frame JButton grid; // names the grid of buttons public ButtonGrid (int width, int length) {// constructor frame.setLayout (new GridLayout (width, length)); // set layout grid = new JButton [width] [length]; // allocate the size of grid for (int y = 0; y <length; y ++) {for (int x = 0; x <width; x ++) {grid [x] [y] = new JButton ("(" + x + "," + y + ")"); // creates new button frame.add (grid [x] [y]); // adds button to grid}} frame.setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE); frame.pack (); // sets appropriate size for frame frame.setVisible (true); // makes frame visible} public static void main (String args) {new ButtonGrid (3, 3); // makes new ButtonGrid with 2 parameters}}

          import javax.swing. JFrame; // imports JFrame library import javax.swing. JButton; // imports JButton library import java.awt. GridLayout; // imports GridLayout library

          public class ButtonGrid {

          JFrame frame = new JFrame (); // creates frame JButton grid; // names the grid of buttons

          public ButtonGrid (int width, int length) {// constructor frame.setLayout (new GridLayout (width, length)); // set layout grid = new JButton [width] [length]; // allocate the size of grid for (int y = 0; y <length; y ++) {for (int x = 0; x <width; x ++) {grid [x] [y] = new JButton ("(" + x + "," + y + ")"); // creates new button frame.add (grid [x] [y]); // adds button to grid}} frame.setDefaultCloseOperation (JFrame. EXIT_ON_CLOSE); frame.pack (); // sets appropriate size for frame frame.setVisible (true); // makes frame visible} public static void main (String args) {new ButtonGrid (3, 3); // makes new ButtonGrid with 2 parameters}

Recommended: