I used GridBagLayout to build a frame for my Swing Student form. This is my code:
package layout;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Student extends JFrame implements ActionListener {
private JPanel panel;
private JLabel titleLabel, nameLabel, ageLabel, addressLabel, genderLabel, courseLabel, timeslotLabel;
private JTextField nameField, ageField;
private JTextArea addressArea;
private JRadioButton maleRadioButton, femaleRadioButton;
private JComboBox coursesComboBox;
public Student() {
setTitle("Đăng ký");
setSize(500, 600);
setResizable(false);
titleLabel = new JLabel("Thông tin sinh viên");
titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
nameLabel = new JLabel("Họ và tên");
nameField = new JTextField(30);
ageLabel = new JLabel("Tuổi");
ageField = new JTextField(30);
addressLabel = new JLabel("Địa chỉ");
addressArea = new JTextArea(3, 20);
addressArea.setLineWrap(true);
addressArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(addressArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
genderLabel = new JLabel("Giới tính");
maleRadioButton = new JRadioButton("Nam");
femaleRadioButton = new JRadioButton("Nữ");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleRadioButton);
genderGroup.add(femaleRadioButton);
courseLabel = new JLabel("Khóa học");
String[] courses = {"Web Application Developer", "Database Administrator", "Network Administrator", "Windows Application Developer"};
coursesComboBox = new JComboBox<>(courses);
panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 10, 10, 10);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
panel.add(titleLabel, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(nameLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
panel.add(nameField, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(ageLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
panel.add(ageField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
panel.add(addressLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridwidth = 2;
panel.add(scrollPane, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
panel.add(genderLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
panel.add(maleRadioButton, gbc);
gbc.gridx = 2;
gbc.gridy = 4;
panel.add(femaleRadioButton, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 1;
panel.add(courseLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 5;
gbc.gridwidth = 2;
panel.add(coursesComboBox, gbc);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new Student();
}
}
And the result:

I do not understand why the femaleRadioButton is lost.
But when I set gbc.width = 1 instead of gbc.width = 2 for scrollPane, the femaleRadioButton will appear.
Why is this?
Its not like you create a new
GridBagConstrainteach time, if you set a certain value once, it just stays the same unless you change it. So the way to solve this is simply by settinggbc.width = 2.