大家谁能帮忙解决两个问题
1.JRadioButtonMenuItem mi3=new JRadioButtonMenuItem("Open");JRadioButtonMenuItem mi4=new JRadioButtonMenuItem("Close"); 这是两个单选菜单项,如何让第一个在一开始就处于选中状态??2
2JPanel panel3=new JPanel();
ButtonGroup bg1=new ButtonGroup();
panel3.add(new JLabel("JRadioButton:"));
String[]JRadioButton={"JRadioButton1","JRadioButton2","JRadioButton3"}; 如何一开始让JRadioButton1处于选中状态??
搜索更多相关的解决方案:
如何
----------------解决方案--------------------------------------------------------
参考代码:
程序代码:
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args) {
ButtonGroup btnGroup = new ButtonGroup();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Items");
menuBar.add(menu);
JRadioButtonMenuItem[] items = {
new JRadioButtonMenuItem("Item1"),
new JRadioButtonMenuItem("Item2"),
};
for (int i = 0; i < items.length; ++i) {
menu.add(items[i]);
btnGroup.add(items[i]);
}
items[0].setSelected(true); // 设置第一个单选项item1为选中状态
JFrame frame = new JFrame("Test");
frame.setJMenuBar(menuBar);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 200, 300, 300);
frame.setVisible(true);
}
}
import java.awt.*;
public class Test {
public static void main(String[] args) {
ButtonGroup btnGroup = new ButtonGroup();
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Items");
menuBar.add(menu);
JRadioButtonMenuItem[] items = {
new JRadioButtonMenuItem("Item1"),
new JRadioButtonMenuItem("Item2"),
};
for (int i = 0; i < items.length; ++i) {
menu.add(items[i]);
btnGroup.add(items[i]);
}
items[0].setSelected(true); // 设置第一个单选项item1为选中状态
JFrame frame = new JFrame("Test");
frame.setJMenuBar(menuBar);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(200, 200, 300, 300);
frame.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------