Help with the basics please
hello, i am trying to create a window that displays a grid of 100 buttons arranged 10 x 10. I then would like to place a couple of buttons nicely beneath this grid that might exit the program or whatever.
my problem here is that i can't control the size of the grid the two nested for loops create and it just fills the entire window and dosen't even stay in a 10 x 10 formation, instead it randomly arranges them to fit the two buttons in at the bottom!
is there some sort of setSize comand i can use so the grid is set to a certain size, maybe of the EAST of the screen?
here is my code so far!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class trialgridextends JFrame
{
public trialgrid()
{
JFrame frame =new JFrame("One Player");
frame.setSize(620, 650);// sets size of window displayed
Container content = frame.getContentPane();
JPanel grid =new JPanel();
content.setLayout(new GridLayout(10, 10));// contains the grids info
content.setSize(100, 100);// this is doing nothing!!!
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
JButton square =new JButton();
square.setBackground(Color.blue);//nested for loops to create 10 x 10 grid of buttons
content.add(square);
}
}
content.add(grid, BorderLayout.NORTH);// trying to create some sort of layout
JPanel form =new JPanel();// second panel that i want to go beneath grid of buttons
form.add(new JButton("OK"));
form.add(new JButton("Exit"));
content.add(form, BorderLayout.SOUTH);// here i'm trying to put south below "north" grid
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoid main (String args[])
{
trialgrid gl =new trialgrid();
}
}
You were not adding buttons to the grid, but to the frame container.
Here is your fixed code :
<code>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class trialgrid extends JFrame
{
public trialgrid()
{
JFrame frame = new JFrame("One Player");
frame.setSize(620, 650); // sets size of window displayed
Container content = frame.getContentPane();
JPanel grid = new JPanel();
grid.setLayout(new GridLayout(10, 10)); // contains the grids info
//content.setSize(100, 100); // this is doing nothing!!!
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
JButton square = new JButton();
square.setBackground(Color.blue); //nested for loops to create 10 x 10 grid of buttons
grid.add(square);
}
}
content.add(grid, BorderLayout.CENTER); // trying to create some sort of layout
JPanel form = new JPanel(); // second panel that i want to go beneath grid of buttons
form.add(new JButton("OK"));
form.add(new JButton("Exit"));
content.add(form, BorderLayout.SOUTH); // here i'm trying to put south below "north" grid
frame.pack();
frame.setVisible(true);
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[])
{
trialgrid gl = new trialgrid();
}
}
</code>
Sorry for incoherent code formatting here is the fixed code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class trialgrid extends JFrame
{
public trialgrid()
{
JFrame frame = new JFrame("One Player");
frame.setSize(620, 650); // sets size of window displayed
Container content = frame.getContentPane();
JPanel grid = new JPanel();
grid.setLayout(new GridLayout(10, 10)); // contains the grids info
//content.setSize(100, 100); // this is doing nothing!!!
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
JButton square = new JButton();
square.setBackground(Color.blue); //nested for loops to create 10 x 10 grid of buttons
grid.add(square);
}
}
content.add(grid, BorderLayout.CENTER); // trying to create some sort of layout
JPanel form = new JPanel(); // second panel that i want to go beneath grid of buttons
form.add(new JButton("OK"));
form.add(new JButton("Exit"));
content.add(form, BorderLayout.SOUTH); // here i'm trying to put south below "north" grid
frame.pack();
frame.setVisible(true);
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[])
{
trialgrid gl = new trialgrid();
}
}
Ya, I'm no expert, but that whole for loop thing doesn't seem like it would work. It makes sense to use the 10 and all to make a ten by ten, but wouldn't you need something that tells it to put the buttons at ( i , j ) or something like that? Do you see what I'm saying?
I'd suggest using 3 panels. Panel 1 has the 10x10 grid of buttons. Panel 2 has your exit etc buttons. Now place panel1 and panel 2 on panel 3. Then add panel 3 to your frame.
Thank you Sashka, i put your code in and it has worked perfectly in assembling the two panels correctly so that they both have a defined location. The only problem i have left is one of my original ones..... i need to be able to control the actual size (height and width) of the grid that is produced by the for loops!!
Because i want to set the size of the window to 620 by 650 pixels i wondered if you could control the size of the panel and then maybe centralize it in the middle so it would have spaces either side and top and bottom?
The thing about the buttons is i am later going to use something like mouseClicked(MouseEvent);
getX(),
getY()
to locate the mouse's location to manipulate actions. So im not sure yet if i need to be able to name each button or get rid of the buttons completely!!
To Flounder, that idea does interest me a lot, i have been thinking i need more panels, maybe East, North and West and fill these with ships, a title and instructions respectively. **To make sense of this i am creating a battleships game** So i need to make another 5 panels really so i'm not sure if i could add all these to a 6th panel?
any code or suggestions greatly appreicated!! Thank you
1.
> The only problem i have left is one of my original
> l ones..... i need to be able to control the actual
> size (height and width) of the grid that is produced
> by the for loops!!
>
> Because i want to set the size of the window to 620
> by 650 pixels i wondered if you could control the
> size of the panel and then maybe centralize it in the
> middle so it would have spaces either side and top
> and bottom?
>
Read More about:- Layout managers. setPreferredSize(), getPreferredSize() etc
Subclass a JPanel so that it returns your preferred Dimensions
2.
> The thing about the buttons is i am later going to
> use something like mouseClicked(MouseEvent);
> getX(),
> getY()
to locate the mouse's location to
> manipulate actions. So im not sure yet if i need to
> be able to name each button or get rid of the buttons
> completely!!
If you want the effect of buttons and don't want to go to all the extra trouble of interpreting which one was clicked from the mouse coordinates, which is going to be a real pain if you resize anything.. you could try something along these lines...
private void fillPanelWithLotsOfButtons( JPanel inPanel )
{
JButton currentButton;
String s = "";
int buttonNumber = 0;
GridLayout grid = new GridLayout( 10, 10, 5, 5 ); // 5Pixel spaces Horizontal and vertical
inPanel.setLayout( grid );
for( int i = 0 ; i < 10 ; i ++ )
{
for( int j = 0; j < 10 ; j ++ )
{
buttonNumber = ( i * 10 ) + j;
s = Integer.toString( buttonNumber).trim();
currentButton = new JButton(); // you might want to add a Subclass of Button that is set to your wanted size....
// Or you could do currentButton = new JButton( s );
currentButton.setActionCommand( s ); // will be "0", "1", "2", ......................................
currentButton.addActionListener( this ); // assumes that this classs implements ActionListener
inPanel.add( currentButton );
}
}
}
// Then Further on in your code
// you can tell which button was pressed fairly easily
public void actionPerformed( ActionEvent event )
{
String myAction = event.getActionCommand();
int buttonNumber = Integer.parseInt( myAction );
System.out.println( "Button Number " + buttonNumber );
}
or something along those sort of lines
> To Flounder, that idea does interest me a lot, i have
> been thinking i need more panels, maybe East, North
> and West and fill these with ships, a title and
> instructions respectively. **To make sense of this i
> am creating a battleships game** So i need to make
> another 5 panels really so i'm not sure if i could
> add all these to a 6th panel?
>
After reading this, I'd suggest using JToggleButton
you could make a custom subclass with appropriate Images, for selected and not selected
> any code or suggestions greatly appreicated!! Thank
> you
Ah i see, i have thrown that in and used:-
Dimension mySize = new Dimension(20,20);
btnCurrent.setPreferredSize(mySize);
to set my button size. I have then renamed each button P0, P1, P2 ...... and E0, E1, E2........ so that in the future i can use some sort of lookup function instead of getX(), and getY() - is this the right idea do you think?
My next trick is i want to somehow get it so that when a user clicks on one my my "ship" buttons the program allows them to then select a button(s - depending on ship length as they are all different sizez) on the grid to allocate that they want a ship to go there. This then has to be saved in location so that other players can play battleships and try to hit the saved ship. If they do i then need a mouseAction function that changes the color of the square!!
As you can see i'm really getting into the hard bit! Do i need to write this in a completely seperate class and then link the two in some sort of hierachy, if so? How do you reccomend going about doing that, i think it will need some kind of array tables and IF statements but i really don't have a clue where to start with this project!!?
