int o = Convert.ToInt32(textBox1.Text);
for (int u = 0; u < o; u++)
{
GroupBox groupBox = new GroupBox();
groupBox.Location = new Point(53, 12 + 60 * u);
groupBox.Size = new Size(734, 52);
groupBox.Text = "专家" + (u+1) + "意见";
string[] radioButtonLabels = { "很低", "低", "较低", "中等", "较高", "高", "很高" };
for (int i = 0; i < 7; i++)
{
RadioButton radioButton = new RadioButton();
radioButton.Text = radioButtonLabels[i];
radioButton.Location = new Point(19 + 100 * i, 26);
radioButton.Size = new Size(95, 16);
groupBox.Controls.Add(radioButton);
}
this.Controls.Add(groupBox);
}
以上是您生成任意数量的groupBox且每个groupBox包含7个radioButton的代码。请问如何获取这些radioButton的选中值并进行计算?
```【以下回答由 GPT 生成】
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class MainFrame extends JFrame {
private List<List<JRadioButton>> radioButtons;
private List<List<String>> selectedValues;
public MainFrame() {
radioButtons = new ArrayList<>();
selectedValues = new ArrayList<>();
int o = Integer.parseInt(textBox1.getText());
for (int u = 0; u < o; u++) {
GroupBox groupBox = new GroupBox();
groupBox.setLocation(new Point(53, 12 + 60 * u));
groupBox.setSize(new Size(734, 52));
groupBox.setText("专家" + (u + 1) + "意见");
String[] radioButtonLabels = {"很低", "低", "较低", "中等", "较高", "高", "很高"};
List<JRadioButton> groupBoxButtons = new ArrayList<>();
List<String> groupBoxValues = new ArrayList<>();
for (int i = 0; i < 7; i++) {
JRadioButton radioButton = new JRadioButton();
radioButton.setText(radioButtonLabels[i]);
radioButton.setLocation(new Point(19 + 100 * i, 26));
radioButton.setSize(new Size(95, 16));
final String value = radioButtonLabels[i]; // 保存选项的值
radioButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 处理选中状态的改变
if (radioButton.isSelected()) {
// 将选中的值存储起来
groupBoxValues.add(value);
}
}
});
groupBox.getControls().add(radioButton);
groupBoxButtons.add(radioButton);
}
radioButtons.add(groupBoxButtons);
selectedValues.add(groupBoxValues);
this.getControls().add(groupBox);
}
}
public void calculate() {
// 对选中的值进行计算
for (int u = 0; u < radioButtons.size(); u++) {
List<JRadioButton> groupBoxButtons = radioButtons.get(u);
List<String> groupBoxValues = selectedValues.get(u);
// 计算每组groupBox中选中值的个数
int count = groupBoxValues.size();
System.out.println("专家" + (u + 1) + "选中了" + count + "个选项。");
}
}
// 其他相关方法和逻辑...
}
在上述示例中,我们增加了一个selectedValues
列表来存储每组groupBox中选中的值。在每个radioButton的ActionListener中,将选中的选项的值存储到对应的groupBoxValues
列表中。我们还添加了一个calculate()
方法用于计算每组groupBox中选中值的个数,您可以在其中根据需要实现具体的计算逻辑。
请注意,上述示例仅为演示目的,您需要根据实际需求进行适当的更改和扩展。
【相关推荐】