I have a JPanel
which uses a BoxLayout
in the X_AXIS
direction. The problem I have is best shown by an image:
As you can see the JPanel
on the left is has been centered rather than aligned at the top. I would like them both to be aligned at the top and stacked left to right, how can I achieve this with this layout manager? The code I have written is as follows:
public GameSelectionPanel(){
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setAlignmentY(TOP_ALIGNMENT);
setBorder(BorderFactory.createLineBorder(Color.black));
JPanel botSelectionPanel = new JPanel();
botSel开发者_如何学编程ectionPanel.setLayout(new BoxLayout(botSelectionPanel, BoxLayout.Y_AXIS));
botSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.red));
JLabel command = new JLabel("Please select your opponent:");
ButtonGroup group = new ButtonGroup();
JRadioButton button1 = new JRadioButton("hello world");
JRadioButton button2 = new JRadioButton("hello world");
JRadioButton button3 = new JRadioButton("hello world");
JRadioButton button4 = new JRadioButton("hello world");
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
botSelectionPanel.add(command);
botSelectionPanel.add(button1);
botSelectionPanel.add(button2);
botSelectionPanel.add(button3);
botSelectionPanel.add(button4);
JPanel blindSelectionPanel = new JPanel();
blindSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.yellow));
blindSelectionPanel.setLayout(new BoxLayout(blindSelectionPanel, BoxLayout.Y_AXIS));
JRadioButton button5 = new JRadioButton("hello world");
JRadioButton button6 = new JRadioButton("hello world");
ButtonGroup group2 = new ButtonGroup();
group2.add(button5);
group2.add(button6);
JLabel blindStructureQuestion = new JLabel("Please select the blind structure:");
blindSelectionPanel.add(blindStructureQuestion);
blindSelectionPanel.add(button5);
blindSelectionPanel.add(button6);
add(botSelectionPanel);
add(blindSelectionPanel);
setVisible(true);
}
Riduidel is correct about setting setAlignmentY
on the GameSelectionPanel
itself, and GridBagLayout
is an excellent alternative. If you prefer to stick with BoxLayout
, the article Fixing Alignment Problems discusses the matter, suggesting "all the components controlled by a left-to-right Boxlayout should generally have the same Y alignment." In your example, add
botSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
blindSelectionPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
Well, the setAlignmentY
method has no effect here, since it acts on the panel considered as a component.
As you have guessed, the layout of contained panels is defined by the layout manager you use.
Unfortunatly, BoxLayout
do not provide the kind of feature you're looking at.
in standard JDK, obviously, the layout of choice for your problem is GridBagLayout
. Although rather hard to understand at first, it will fast reveal to you its power in components arrangement.
using the useful GBC class, your components could be arranged as such :
setLayout(new GridBagLayout(this));
add(botSelectionPanel, new GBC(0,1).setAnchor(TOP));
add(blindSelectionPanel, new GBC(0,2).setAnchor(TOP));
or I think so ;-)
精彩评论