I want to layout 6 components, vertically aligned in one column. In addition, I want a blank space of 200 pixels before the first component in the column. I have the following code:
public class MongoMusicApplet extends JApplet{
//*****main menu objects********
private JPanel mainMenuPanel;
private JButton buildingButton;
private JTextField text;
public void init(){
setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
mainMenuPanel=new JPanel();
buildingButton=new JButton("Graph-Building Mode");
text=new JTextField(20);
JLabel label1=new JLabel("Help us improve by entering");
JLabel label2=new JLabel("OR");
JLabel label3=new JLabel("Enter the name of an artist");
JLabel label4=new JLabel("to enter Discovery Mode");
mainMenuPanel.add(Box.createRigidArea(new Dimension(0,200)));
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
mainMenuPanel.add(label1);
buildingButton.setAlignmentX(Component.CENTER_ALIGNMENT);
mainMenuPanel.add(buildingButton);
...add all other components in this order: label 2, label3, text, label4...
add(mainMenuPanel);
}
}
And I get the following layout:
So first off, it seems that the createRigidArea is creating space before the column starts, and also in between some of the components in the column. Is there a way to make it only create space before the first component in the column?
And second, since I chose the alignment to be Y_AXIS, why are the components not arranged vertically, with one component per row? I also tried to use GridLayout(0,1), but that gave me the exact same layout. How can I force these components into one vertical column?