How to Make a GUI Grid in Java
The Grid does nothing special at this stage, but with a little bit of research, you can add action listeners and a bit of logic to make a simple 2D game like tic-tac-toe, or more complicated ones like Battleship. Note: This article uses...
Method 1 of 1:
Steps Code
- 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); } ...
- Adding 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]); } } ...
- Making a button grid 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
Update 05 March 2020
You should read it
- Learn about Public IP address
- Frame Relay, solutions for businesses and corporations (Part 1)
- 4 ways to measure FPS frame rate (Frame Per Second) in PC game
- How to show the alignment frame in Word
- Download and experience iOS 11.4 Public beta is more stable than Developer version, and has AirPlay 2
- How to 'convert' from the iOS Public Beta version to the old version
- How to create a Public Account on Viber
- How do Private Network and Public Network on Windows differ?
- 4 potential risks of using public wifi that users should know
- How to frame text in Word
- Instructions for updating iOS 14 public beta have just been released
- Canonical releases Ubuntu Frame for embedded screen developers
Maybe you are interested
The new Atlas robot can perform movements that are impossible for humans 6 apps you should install in the new year 2021 for a more exciting start Limitations of indexes in MongoDB How to set the dark background interface for Google Docs 8 new features are about to be updated on Facebook How to mass delete status, remove Facebook tag in one click