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
4 ★ | 2 Vote
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
How to draw a frame around text using the Snipping Tool
Instructions for installing .NET Framework 3.5 on Windows 10
How to combine photos in Word, combine 2 photos into 1 frame
China develops the world's fastest vision chip, 10,000 frames per second
How to fix Microsoft .NET Framework errors
How to fix iPhone with blue frame 99% effective