How to Make a GUI Grid in Java
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
- 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
May be interested
- Which career Java programming options are waiting for you?java programmers are experts in the java programming language. as of 2018, there are many job opportunities for java programmers. with an expected growth rate of 19% for the period 2014-2024, the career prospects for the java language are really bright.
- Inviting GRID 2 racing game worth $ 29.99, is freegrid 2 is a speed racing game of the 2nd version of grid game developed by codemasters.
- How to install Java on a Raspberry Pithere are two different java implementations, oracle java and openjdk. this tutorial explains how to install java (openjdk) on a raspberry pi with the latest raspbian operating system running on it.
- How to fix the error does not install Javamore and more applications and websites require java installation before use. unfortunately, sometimes you can't install java or install it, but it doesn't work. the article will provide ways to fix errors that do not install java on your computer.
- How to Tune a Java Virtual Machine (JVM)the java virtual machine (jvm) runs your java programs. sometimes the default configuration that the jvm comes with may not be the most efficient for your program.
- Download GRID Autosport racing game worth $ 39.99, free of chargein the game, players will play the role of a racer who begins to build his own career by participating in competitions for different teams and sponsors to gain high achievements and experience.
- What is the difference between Go and Java?is golang better than java? is golang harder than java? can golang replace java? this comparison will give you the answer.
- How to Fix Javajava is a computing platform that allows you to play games and view videos on your computer. you can tell that your computer is having problems with java if you see java errors appear when you try to run a program or visit a website that...
- How to Compile a Java Programthis wikihow teaches you how to turn your java source code into an executable app using a local and online compiler. if you're using a computer, the most common way to compile java code is using java software development kit (java sdk)...
- Oracle wants to turn Java EE into fully open sourcethis week, oracle announced plans to move java ee project management to an open source platform, like apache or eclipse.