GridBagLayout:为什么我的femaleRadioButton不可见?

问题描述 投票:0回答:1

我使用 GridBagLayout 为我的 Swing Student 表单构建了一个框架。这是我的代码:

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();
    }
}

结果:

enter image description here

我不明白为什么 FemaleRadioButton 丢失了。 但是当我为scrollPane设置

gbc.width = 1
而不是
gbc.width = 2
时,femaleRadioButton就会出现。 这是为什么?

java swing gridbaglayout
1个回答
0
投票

“但是当我为scrollPane设置gbc.width = 1而不是gbc.width = 2时,femaleRadioButton将会出现。”

这不像你每次都创建一个新的

GridBagConstraint
,如果你设置了某个值一次,它就会保持不变,除非你改变它。所以解决这个问题的方法很简单,只需设置
gbc.width = 2

© www.soinside.com 2019 - 2024. All rights reserved.