课后,我查阅相关学习资料和Java API制作了以下界面,界面包含了单选按钮(JRadioButton)、复选框(JCheckBox)、组合框(JComboBox)、单行文本输入框(JTextField)以及对话框(JDialog),为label标签设置了背景颜色以及为按钮添加了事件响应。
可以单击下拉菜单选择适应的年月、婚姻状况等,滚动条设置的最大显示数目为五个,单击提交按钮弹出提交页面的对话框,单击对话框中的完成按钮,程序停止。界面用坐标对各个组件进行了定位。
其中有一些要注意的地方:
1、单选框要实现多选一,需要将他们放入一个按钮组中,例如:
ButtonGroup bg = new ButtonGroup(); //创建按钮组
JRadioButton jrb1 = new JRadioButton();
JRadioButton jrb2 = new JRadioButton();
bg.add(jrb1);
bg.add(jrb2);
2、设置背景颜色时,JLabel的默认opaque为false,改变背景颜色用setBackground(Color)即可,要看到效果则需要setOpaque(true)。
3、setBounds(x,y,,width,height);
x:组件在容器X轴上的起点
y:组件在容器Y轴上的起点
width:组件的长度
height:组件的高度
4、其他组件的基本代码可参见以下源码
1 import java.awt.Color; 2 import java.awt.Font; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 6 import javax.swing.ButtonGroup; 7 import javax.swing.JButton; 8 import javax.swing.JCheckBox; 9 import javax.swing.JComboBox; 10 import javax.swing.JDialog; 11 import javax.swing.JFrame; 12 import javax.swing.JLabel; 13 import javax.swing.JPasswordField; 14 import javax.swing.JRadioButton; 15 import javax.swing.JTextField; 16 17 public class register { 18 19 public static void main(String[] args) { 20 // TODO Auto-generated method stub 21 JFrame frame = new JFrame("创建您的征婚档案"); 22 frame.setLayout(null); 23 24 JLabel label0 = new JLabel(" 个人资料:"); 25 label0.setBounds(0,10,450,25); 26 label0.setFont(new Font("",Font.BOLD,15)); 27 label0.setBackground(Color.ORANGE); 28 label0.setOpaque(true); 29 frame.add(label0); 30 31 JLabel label1 = new JLabel("性 别:"); 32 label1.setBounds(40,55,120,15); 33 frame.add(label1); 34 35 JLabel label2 = new JLabel("出生日期:"); 36 label2.setBounds(40,95,120,15); 37 frame.add(label2); 38 39 JLabel label3 = new JLabel("工作地区:"); 40 label3.setBounds(40,135,120,15); 41 frame.add(label3); 42 43 JLabel label4 = new JLabel("婚姻状况:"); 44 label4.setBounds(40,175,120,15); 45 frame.add(label4); 46 47 JLabel label5 = new JLabel("学 历:"); 48 label5.setBounds(40,215,120,15); 49 frame.add(label5); 50 51 JLabel label6 = new JLabel("每月收入:"); 52 label6.setBounds(40,255,120,15); 53 frame.add(label6); 54 55 JLabel label7 = new JLabel(" 您的账号信息:"); 56 label7.setBounds(0,310,450,25); 57 label7.setFont(new Font("",Font.BOLD,15)); 58 label7.setBackground(Color.ORANGE); 59 label7.setOpaque(true); 60 frame.add(label7); 61 62 JLabel label8 = new JLabel("手 机 号:"); 63 label8.setBounds(40,355,120,15); 64 frame.add(label8); 65 66 JLabel label9 = new JLabel("密 码:"); 67 label9.setBounds(40,395,120,15); 68 frame.add(label9); 69 70 JLabel label10 = new JLabel("昵 称:"); 71 label10.setBounds(40,435,120,15); 72 frame.add(label10); 73 74 ButtonGroup bg = new ButtonGroup(); 75 JRadioButton man = new JRadioButton("男"); 76 man.setBounds(110,50,40,25); 77 man.setSelected(true); 78 JRadioButton woman = new JRadioButton("女"); 79 woman.setBounds(185,50,40,25); 80 bg.add(man); 81 bg.add(woman); 82 frame.add(man); 83 frame.add(woman); 84 85 JComboBox data1 = new JComboBox(); 86 data1.addItem("1994年"); 87 data1.addItem("1995年"); 88 data1.addItem("1996年"); 89 frame.add(data1); 90 data1.setBounds(110,90,80,25); 91 92 String[] month = {"12月","11月","10月","9月","8月","7月","6月", 93 "5月","4月","3月","2月","1月"}; 94 JComboBox data2 = new JComboBox(month); 95 data2.setMaximumRowCount(5); 96 frame.add(data2); 97 data2.setBounds(200,90,80,25); 98 JComboBox data3 = new JComboBox(); 99 data3.addItem("30日"); 100 data3.addItem("29日"); 101 data3.addItem("28日"); 102 frame.add(data3); 103 data3.setBounds(290,90,80,25); 104 105 JTextField area = new JTextField("山西省"); 106 frame.add(area); 107 area.setBounds(110,130,100,25); 108 JTextField city = new JTextField("请选择"); 109 frame.add(city); 110 city.setBounds(220,130,80,25); 111 112 JComboBox marry = new JComboBox<>(); 113 marry.addItem("已婚"); 114 marry.addItem("未婚"); 115 frame.add(marry); 116 marry.setBounds(110,170,120,25); 117 118 JComboBox study = new JComboBox<>(); 119 study.addItem("大学本科"); 120 study.addItem("研究生"); 121 study.addItem("博士"); 122 frame.add(study); 123 study.setBounds(110,210,120,25); 124 125 JTextField money = new JTextField(); 126 frame.add(money); 127 money.setBounds(110, 250,160, 25); 128 129 JTextField number = new JTextField(); 130 frame.add(number); 131 number.setBounds(110, 350,160, 25); 132 133 JPasswordField password = new JPasswordField(); 134 frame.add(password); 135 password.setBounds(110, 390,160, 25); 136 137 JTextField name = new JTextField(); 138 frame.add(name); 139 name.setBounds(110, 430,160, 25); 140 141 JCheckBox cb = new JCheckBox("阅读并同意此服务条款和隐私政策"); 142 cb.setBounds(100,500,260,25); 143 frame.add(cb); 144 145 //点鸡提交弹出的页面设置 146 JDialog dlg = new JDialog(frame,"提交成功"); 147 dlg.setBounds(600,220,200,180); 148 dlg.setLayout(null); 149 dlg.setModal(true); 150 151 JButton btn = new JButton("提交"); 152 frame.add(btn); 153 btn.setBounds(180,550,75,25); 154 155 //提交事件响应 156 btn.addActionListener(new ActionListener() { 157 158 @Override 159 public void actionPerformed(ActionEvent arg0) { 160 // TODO Auto-generated method stub 161 dlg.setVisible(true); 162 } 163 }); 164 165 //创建对话框中退出按钮并添加事件响应 166 JButton bt = new JButton("完成"); 167 dlg.add(bt); 168 bt.setBounds(50,50,80,30); 169 bt.addActionListener(new ActionListener() { 170 171 @Override 172 public void actionPerformed(ActionEvent arg0) { 173 // TODO Auto-generated method stub 174 System.exit(0); 175 } 176 }); 177 178 179 frame.setBounds(480,15,450,675); 180 frame.setVisible(true); 181 182 183 184 185 } 186 187 }
效果图:
*头像就是本人照片