我对动态复选框有疑问。I want a combobox to show beside the checbox/checkboxes when one or more is selected. 但是我只得到一个组合框,每当我选择一个新的复选框时,它就会四处移动。那么任何人都可以通过告诉我出了什么问题以及如何获得多个组合框来帮助我吗?
for(int i = 0; i < names.length; i++) {
// ParameterField
JTextField fieldParam = new JTextField();
fieldParam.setText(names[i]);
fieldParam.setEditable(false);
addElement(contentPanel, fieldParam, 25, yPos, 100, 20);
// ValueField
JTextField fieldValue = new JTextField();
fieldValue.setText("" + values[i]);
fieldValue.setEditable(false);
addElement(contentPanel, fieldValue, 160, yPos, 100, 20);
//RadioButtonField
final JCheckBox checkboxes = new JCheckBox();
checkboxes.setActionCommand(names[i]);
checkboxes.addActionListener(this);
addElement(contentPanel, checkboxes, 325, yPos, 100, 20);
final int checkBoxIndex = i;
checkboxes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae2) {
if (checkboxes.isSelected()) {
comboProcent.setEnabled(true);
comboProcent.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"2%", "5%", "10%"}));
addElement(contentPanel, comboProcent, 435, 50 + checkBoxIndex * 25, 80, 20);
setVisible(true);
}
if (!checkboxes.isSelected()) {
contentPanel.remove(comboProcent);
}
System.out.println("checkbox is: " + checkBoxIndex);
}
});
yPos = yPos + 25;
}