I have 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 a radio button, I tried to use GridLayout, but it broke the position of "Yes" and "No". And for the back and next buttons, horizontal alignment does not work ( btnPanel.setAlignmentX(RIGHT_ALIGNMENT); ), apparently. Any solutions would be much appreciated, I was stuck for too long. Thanks
- EDIT -
This works fine:
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.LINE_AXIS)); btnPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); btnPanel.add(Box.createHorizontalGlue());
therefore, the problem with the buttons is resolved. However, it is still impossible to fix the radio buttons.
- EDIT 2 -
Fixed background for buttons using setOpaque(false);
source share