用java 中的JRadioButton的创建7个按钮
时间: 2024-02-20 14:56:22 浏览: 59
可以使用以下代码创建7个JRadioButton按钮:
```
import javax.swing.*;
public class MyRadioButtons {
public static void main(String[] args) {
JFrame frame = new JFrame("My Radio Buttons");
JPanel panel = new JPanel();
JRadioButton button1 = new JRadioButton("Button 1");
JRadioButton button2 = new JRadioButton("Button 2");
JRadioButton button3 = new JRadioButton("Button 3");
JRadioButton button4 = new JRadioButton("Button 4");
JRadioButton button5 = new JRadioButton("Button 5");
JRadioButton button6 = new JRadioButton("Button 6");
JRadioButton button7 = new JRadioButton("Button 7");
// Add the radio buttons to a ButtonGroup so only one can be selected at a time
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
group.add(button5);
group.add(button6);
group.add(button7);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
panel.add(button7);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
```
这将创建一个包含7个JRadioButton按钮的窗口。
阅读全文