Problem with Layouts
Hi All,
I've already posted this message in the New to Java Forum, but got no reply, so Ive decided that maybe this forum is more appropriated.
First of all, I'm pretty new to java, specially the visual side of it =)
After creating a primitive version of minesweeper for an university project, I've decided to port a little game I've made before in Gambas (a BASIC under linux). The main problem I'm having with it is the layout of all the buttons I have to use.
What I'm trying to do is this: http://happypenguin.org/images/thumbs/Glls.png
But the best I got was: http://img267.imageshack.us/img267/7789/brutto8tk.jpg
To get this I've used two BoxLayouts one inside the other
This is the code I've used
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BoxLayoutDemo2implements ItemListener{
protectedstaticint NUM_YCOMPS = 11;
protectedstaticint NUM_XCOMPS = 11;
protectedstaticfloat[] xAlignment ={
Component.LEFT_ALIGNMENT,
Component.CENTER_ALIGNMENT,
Component.RIGHT_ALIGNMENT};
protectedstaticfloat[] hue ={0.0f, 0.33f, 0.67f};
protectedstaticboolean restrictSize =true;
publicvoid populateContentPane(Container contentPane){
JPanel panel2 =new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
JButton bldComponent[][] =new JButton[NUM_XCOMPS][NUM_YCOMPS];
//Create the rectangles.
int shortSideSize = 15;
for (int j =0;j<NUM_YCOMPS;j++){
JPanel panel =new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
if (j%2!=0)
for (int i = 0; i >< NUM_XCOMPS; i++){
bldComponent[i][j] =new JButton();
if (i%2==0)
bldComponent[i][j].setPreferredSize(new Dimension(5,12));
else
bldComponent[i][j].setPreferredSize(new Dimension(12,12));
panel.add(bldComponent[i][j]);
}
else
for (int i = 0; i < (NUM_XCOMPS-1)/2; i++){
bldComponent[i][j] =new JButton();
bldComponent[i][j].setPreferredSize(new Dimension(12,5));
panel.add(bldComponent[i][j]);
}
panel2.add(panel);
}
contentPane.add(panel2, BorderLayout.CENTER);
}
//-End Of Interesting part -//
publicvoid itemStateChanged(ItemEvent e){
if (e.getStateChange() == ItemEvent.SELECTED){
restrictSize =true;
}else{
restrictSize =false;
}
System.out.println("Ciao");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
privatestaticvoid createAndShowGUI(){
//Create and set up the window.
JFrame frame =new JFrame("BoxLayoutDemo2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
BoxLayoutDemo2 demo =new BoxLayoutDemo2();
demo.populateContentPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
publicstaticvoid main(String[] args){
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
}
Is a modified version of a Java example you can find in the tutorials
Can anyone help me?
Thanks
ps: no, I'm not a native english speaker, sorry

