我有一个扩展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);
}
答案 0 :(得分:1)
我使用 CardLayout
解决了我的问题
我为自己的CardLayout创建了一个JPanel,为每种类型的工资创建了另外两个JPanel
我在每个单选按钮上使用了两个动作侦听器来切换到下一张卡