我有一个扩展JPanel的类,我将其用作GUI来保存工人信息,包括薪水,单选按钮可以选择两个选项。

选择第一个时,无需在面板上进行任何操作。

但是,如果选择了第二个单选按钮,我们必须显示一些额外的文本字段,并且*这里出现了问题-我的面板无法调整大小以适合组件,并且完全混乱。

我尝试实现观察者模式,但是它不起作用或做错了
在这里,按照我班的主要部分。

public class EmpDetails extends JPanel {

public EmpDetails() {

    matriculeLab = new JLabel("MATRICULE :");
    matriculeTxt = new JTextField(10);


    nomLab = new JLabel("Nom :");
    nomTxt = new JTextField(30);


    serviceLab = new JLabel("SERVICE :");
    serviceDrBx = new JComboBox<>();

    fonctionLab = new JLabel("FONCTION :");
    fonctionDrBx = new JComboBox<>();


    dateEmbLab = new JLabel("DATE D'EMBAUCHE :");
    dateEmb = DatePick.getDatePick();

    ///  This is the first radio button

    salaireFxBtn = new JRadioButton("FIXE ");
    salaireFxBtn.setSelected(true);


    /// This is the second radio button that shows the hiden elements

    salaireChngBtn = new JRadioButton("CHANGEANT ");


    bg.add(salaireFxBtn);
    bg.add(salaireChngBtn);

    btnPanel.setLayout(new GridLayout(2, 1));
    btnPanel.add(salaireFxBtn);
    btnPanel.add(salaireChngBtn);


    salaireLab = new JLabel("MONTANT DU SALAIRE:");
    montatSalFix = new JFormattedTextField(NumberFormat.getIntegerInstance());


    /// This is the panel holding the hidden elements its shown when radio button 2 is clicked

    salPanel.setLayout(new GridLayout(3, 2));

    salaire1 = new JLabel("MONTANT SALAIRE 1:");
    montantSal1 = new JFormattedTextField(NumberFormat.getIntegerInstance());

    salPanel.add(salaire1);
    salPanel.add(montantSal1);

    periodeLab1 = new JLabel("DUREE 1 :");
    periode1 = new JFormattedTextField(NumberFormat.getIntegerInstance());

    salPanel.add(periodeLab1);
    salPanel.add(periode1);

    salaire2 = new JLabel("MONTANT SALAIRE 2:");
    montantSal2 = new JFormattedTextField(NumberFormat.getIntegerInstance());

    salPanel.add(salaire2);
    salPanel.add(montantSal2);

    periodeLab2 = new JLabel("DUREE 2 :");
    periode2 = new JFormattedTextField(NumberFormat.getIntegerInstance());

    salPanel.add(periodeLab2);
    salPanel.add(periode2);

    salaire3 = new JLabel("MONTANT SALAIRE 3:");
    montantSal3 = new JFormattedTextField(NumberFormat.getIntegerInstance());

    salPanel.add(salaire3);
    salPanel.add(montantSal3);

    periodeLab3 = new JLabel("DUREE 3 :");
    periode3 = new JFormattedTextField(NumberFormat.getIntegerInstance());

    salPanel.add(periodeLab3);
    salPanel.add(periode3);

    salPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
    salPanel.setVisible(false);

    indemLab = new JLabel("INDEMNITE :");
    indemnite = new JFormattedTextField(NumberFormat.getIntegerInstance());



    salaireChngBtn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            if (salaireChngBtn.isSelected()) {
                salPanel.setVisible(true);
            }
        }
    });

    salaireFxBtn.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            if (salaireFxBtn.isSelected()) {
                salPanel.setVisible(false);
            }

        }
    });

    layoutComponents();
    setVisible(true);
}

private void layoutComponents() {
    setLayout(new GridBagLayout());
    GridBagConstraints gb = new GridBagConstraints();

    //   The rest is an implementation of the GridBagLayout to design the interface
    }
}


这是布局

private void layoutComponents() {
    setLayout(new GridBagLayout());
    GridBagConstraints gb = new GridBagConstraints();

    gb.fill = GridBagConstraints.NONE;
    gb.weightx = 1;
    gb.weighty = 1;
    gb.insets = new Insets(7, 7, 7, 7);

    // LINE 01 //
    gb.gridx = 0;
    gb.gridy = 0;

    add(matriculeLab, gb);
    gb.gridx++;
    add(matriculeTxt, gb);

    gb.gridx++;
    add(nomLab, gb);
    gb.gridx++;
    add(nomTxt, gb);

    // LINE 02 //
    gb.gridx = 0;
    gb.gridy = 1;

    add(serviceLab, gb);
    gb.gridx++;
    add(serviceDrBx, gb);

    gb.gridx++;
    add(fonctionLab, gb);
    gb.gridx++;
    add(fonctionDrBx, gb);

    // LINE 03 //
    gb.gridx = 0;
    gb.gridy++;

    add(dateEmbLab, gb);
    gb.gridx++;
    add(dateEmb, gb);

    // LINE 04 //
    gb.gridx = 0;
    gb.gridy++;

    add(btnPanel, gb);

    // LINE 05 //
    gb.gridx = 0;
    gb.gridy++;

    add(salaireLab, gb);
    gb.gridx++;
    add(montatSalFix, gb);



    // LINE 0 //
    gb.gridx = 0;
    gb.gridy++;

    add(salPanel, gb);

    // LINE 09 //
    gb.gridx = 0;
    gb.gridy++;

    add(indemLab, gb);
    gb.gridx++;
    add(indemnite, gb);
}

最佳答案

我使用CardLayout解决了我的问题
我为自己的CardLayout创建了一个JPanel,并且为每种工资类型创建了另外两个JPanels
我在每个单选按钮上使用了两个动​​作侦听器来切换到下一张卡

10-02 05:45