I have got a window that looks like window1 and I would like it to look like window2:

This is my code:
String q = "Have you used GUI before?";
JLabel textLabel2 = new JLabel(
"<html><div style=\"text-align: center;\">" + q + "</html>", SwingConstants.CENTER);
add(textLabel2, BorderLayout.NORTH);
JPanel radioPanel = new JPanel();
add(radioPanel, BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
add(btnPanel, BorderLayout.SOUTH);
For the radio-buttons, I tried to use GridLayout, but it broke the position of “Yes” and “No”. And for the “back” and “next” buttons, horizontal alignment did not work (btnPanel.setAlignmentX(RIGHT_ALIGNMENT);), apparently. Any solutions will be highly appreciated, I’m stuck with this bit way too long. Thanks
–EDIT–
That is working perfectly fine:
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS));
btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
btnPanel.add(Box.createHorizontalGlue());
so the buttons problem is solved.
However, still can’t get the radio-buttons fixed.
–EDIT 2–
Fixed the background for the radio-buttons using setOpaque(false);
What do you mean by it “broke” the position of “yes” and “no” as a GridLayout should work just fine. I’d give it 1 column and 2 (or 0 for variable number of) rows via
new GridLayout(0, 1). Be sure that its opaque property is set as false by doingradioPanel.setOpaque(false);. This way it will show the background color of the container that it sits in. You may need to make the JRadioButtons non-opaque as well, I’m not sure.Your btnPanel could use a BoxLayout and use Box.createGlue() to push the buttons over to the right side.
Most importantly — if you haven’t yet done so, please read the tutorials on use of the Swing layout managers which you can find here.