Question:
How to build a grid layout in java?
Cheetoz
2011-07-31 17:37:46 UTC
(In java) i would like to build a board layout in java for the starting point of a minesweeper game. All i would like to create is a grid layout with the placement of mines in random places in the layout.
Three answers:
Vaibhav
2011-07-31 18:01:06 UTC
An Example

import javax.swing.JFrame;

class MineSweeper extends JFrame

{

JButton b[][]=new JButton[10][10];

MineSweeper()

{

init();

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(500,500);

setVisible(true);

}

public void init()

{

setLayout(new GridLayout(10,10));

int i,j;

for(i=0;i<10;i++)

{

for(j=0;j<10;j++)

{

b[i][j]=new JButton(i+":"+j);

add(b[i][j]);

}

}

}

}

now you need to create an object of this class

and it will display a 10*10 grid of buttons
Maninder Pal Singh
2011-07-31 17:51:39 UTC
Just compile this program as GridLayout.java





// Demonstrate GridLayout



import java.awt.*;



public class GridLayoutDemo extends Frame

{

int n = 4;

public void init()

{

setLayout(new GridLayout(n, n));



setFont(new Font("SansSerif", Font.BOLD, 20));

for(int i = 0; i < n; i++)

{

for(int j = 0; j < n; j++)

{

int k = i * n + j;

if(k >= 0)

add(new Button("" + k));

}

}

}

// thankyou... mani_khangura2000@yahoo.com

}
?
2017-01-02 13:17:03 UTC
Why dont you employ JBuilder and make an utility rather. this way, you only drag the buttons and code is made. you only adjust it. i will email you one tester I made for a student in case you go with for.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...