实验十三:窗口设计
实验代码:
package 实验13; import java.awt.Container; import java.awt.FlowLayout; import javax.swing.*; class Shiyan13 extends JFrame { public Shiyan13() { JFrame a=new JFrame(); a.setLayout(new FlowLayout()); a.setTitle("Introduction"); a.setSize(400,200); a.setLocation(300,240); JLabel c=new JLabel("姓名:"); a.add(c); JLabel d=new JLabel("张灿"); a.add(d); JRadioButton radio1, radio2; a.add(new JLabel("性别:")); ButtonGroup group = new ButtonGroup(); //单选按钮所在的组 radio1 = new JRadioButton("男"); //创建单选按钮 radio2 = new JRadioButton("女"); group.add(radio1); group.add(radio2); a.add(radio1); a.add(radio2); a.add(new JLabel("民族:")); //创建下拉框 String proList[] = { "汉族","彝族","苗族","回族" ,"其他"}; JComboBox comboBox; Container conPane = getContentPane(); comboBox = new JComboBox(proList); comboBox.setEditable(true); conPane.add(comboBox); a.add(conPane); JCheckBox checkBox1, checkBox2, checkBox3,checkBox4; //创建选择框 a.add(new JLabel(" 爱好: ")); checkBox1 = new JCheckBox(" 运动 "); checkBox2 = new JCheckBox(" 听音乐 "); checkBox3 = new JCheckBox(" 唱歌 "); checkBox4 = new JCheckBox(" 旅游 "); a.add(checkBox1); a.add(checkBox2); a.add(checkBox3); a.add(checkBox4); a.add(new JLabel(" 专业: ")); String str[]= {"网络工程","软件工程","物联网","计算机科学与技术"}; JList list = new JList(str); list.setVisibleRowCount(3);//设置可视的行数 a.add(new JScrollPane(list)); a.setVisible(true); } } public class Shiyan13 { public static void main(String[] args) { new Shiyan13(); } }
心得体会
1、单选框
JRadioButton j1=new JRadioButton("男"); JRadioButton j2=new JRadioButton("女");
2、有些函数要放到主函数外才对
3、 JFrame和JDialog类拥有一个setDefaultCloseOperation()方法,可用于窗口/对话框关闭处理。