JAVA 註冊介面
阿新 • • 發佈:2018-11-19
Java上機作業
完成註冊介面
程式碼如下:
1 import java.awt.Color; 2 import java.awt.Dimension; 3 import java.awt.FlowLayout; 4 import java.awt.Font; 5 import java.awt.Rectangle; 6 import java.awt.event.ItemEvent; 7 import java.awt.event.ItemListener; 8 9 import javax.swing.BorderFactory; 10 importjavax.swing.Box; 11 import javax.swing.ButtonGroup; 12 import javax.swing.JButton; 13 import javax.swing.JCheckBox; 14 import javax.swing.JComboBox; 15 import javax.swing.JFrame; 16 import javax.swing.JLabel; 17 import javax.swing.JPanel; 18 import javax.swing.JPasswordField; 19 import javax.swing.JRadioButton;20 import javax.swing.JTextField; 21 22 23 //日曆下拉框 24 class YearMonthDay extends JFrame { 25 26 private final int STARTYEAR = 2000;// 年份的開始值 27 private final int ENDYEAR = 2020;// 年份的結束值 28 29 //JPanel contentPane; 30 // 年月日的選擇框 31 JComboBox cboYear = new JComboBox(); 32 JComboBox cboMonth = newJComboBox(); 33 JComboBox cboDay = new JComboBox(); 34 // 年月日標籤 35 JLabel jLabel1 = new JLabel(); 36 JLabel jLabel2 = new JLabel(); 37 JLabel jLabel3 = new JLabel(); 38 39 public YearMonthDay() { 40 41 setDefaultCloseOperation(EXIT_ON_CLOSE); 42 jbInit(); 43 44 } 45 46 private void jbInit() { 47 48 // contentPane = (JPanel) getContentPane(); 49 // contentPane.setLayout(null); 50 // setSize(new Dimension(400, 300)); 51 // setTitle("年月日下拉列表級聯"); 52 // 年的下拉選擇框 53 cboYear.setFont(new java.awt.Font("Dialog", Font.BOLD, 13)); 54 cboYear.setBounds(new Rectangle(0, 0, 55, 18)); 55 // 月的下拉選擇框 56 cboMonth.setFont(new java.awt.Font("Dialog", Font.BOLD, 13)); 57 cboMonth.setBounds(new Rectangle(80, 0, 45, 18)); 58 cboMonth.addItemListener( new DateItemAdapter(this)); 59 // 日的下拉選擇框 60 cboDay.setFont(new java.awt.Font("Dialog", Font.BOLD, 13)); 61 cboDay.setBounds(new Rectangle(150, 0, 45, 18)); 62 // cboDay.setEditable(true); 63 // 年的label 64 jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 15)); 65 jLabel3.setText("年"); 66 jLabel3.setBounds(new Rectangle(60, 0, 20, 20)); 67 // 月的label 68 jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 15)); 69 jLabel2.setText("月"); 70 jLabel2.setBounds(new Rectangle(130, 0, 20, 20)); 71 // 日的label 72 jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 15)); 73 jLabel1.setText("日"); 74 jLabel1.setBounds(new Rectangle(200, 0, 20, 20)); 75 76 // contentPane.add(cboYear); 77 // contentPane.add(cboMonth); 78 // contentPane.add(cboDay); 79 // contentPane.add(jLabel3); 80 // contentPane.add(jLabel2); 81 // contentPane.add(jLabel1); 82 83 // 新增初始值 84 AddInfo(); 85 } 86 87 private void AddInfo() { 88 // 年下拉選擇框 89 for (int i = STARTYEAR; i < ENDYEAR; i++) { 90 cboYear.addItem("" + i); 91 } 92 93 // 月下拉選擇框 94 for (int i = 0; i < 12; i++) { 95 cboMonth.addItem("" + (i + 1)); 96 } 97 98 // 日下拉選擇框 99 for (int j = 0; j < 31; j++) { 100 cboDay.addItem("" + (j + 1)); 101 } 102 } 103 104 public void cboMonth_itemStateChanged(ItemEvent e) { 105 106 Object obj = cboMonth.getSelectedItem();// 取得選中月份 107 108 if (obj != null) { 109 cboDay.removeAllItems();// 清空日的下拉列表框 110 111 int month = Integer.valueOf(obj.toString()); 112 int days = 31; 113 if (month == 4 || month == 6 || month == 9 || month == 11) { 114 days = 30; 115 } else if (month == 2) { 116 // 取得選中年份 117 int year = Integer.parseInt(cboYear.getSelectedItem() 118 .toString()); 119 if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { 120 // 是閏年 121 days = 29; 122 } else { 123 // 不是閏年 124 days = 28; 125 } 126 }// if 127 128 for (int j = 0; j < days; j++) { 129 cboDay.addItem("" + (j + 1)); 130 }// for 131 }// if 132 }// if 133 }// end class 134 //事件監聽器 135 class DateItemAdapter implements ItemListener { 136 137 private YearMonthDay adaptee; 138 139 DateItemAdapter(YearMonthDay adaptee) { 140 this.adaptee = adaptee; 141 } 142 public void itemStateChanged(ItemEvent e) { 143 adaptee.cboMonth_itemStateChanged(e); 144 } 145 } 146 147 148 149 public class Text extends JFrame { 150 151 /** 152 * @param args 153 */ 154 public static void main(String[] args){ 155 // TODO Auto-generated method stub 156 JFrame jf = new JFrame("註冊"); 157 jf.setDefaultCloseOperation(EXIT_ON_CLOSE);// 關閉 158 jf.setSize(800, 600); // 視窗大小 159 jf.setLocation(200, 200); // 視窗位置 160 JPanel pan = new JPanel(); 161 jf.add(pan); 162 jf.setVisible(true); 163 pan.setLayout(null); 164 // 元件字型 165 Font f = new Font("宋體", Font.BOLD, 14); 166 167 // 新增標籤和文字框 168 JLabel l_account = new JLabel("賬號"); 169 l_account.setBounds(20,0,100,100); //設定佈局 170 pan.add(l_account); 171 l_account.setFont(f); 172 JTextField tf_account = new JTextField(10); 173 tf_account.setBounds(100, 40, 150, 25); 174 pan.add(tf_account); 175 176 177 JLabel l_password = new JLabel("密碼"); 178 l_password.setBounds(20, 40, 100, 100); 179 pan.add(l_password); 180 l_password.setFont(f); 181 182 JPasswordField password = new JPasswordField(20); 183 password.setEchoChar('*'); 184 password.setBounds(100, 80, 150, 25); 185 pan.add(password); 186 187 188 189 JLabel l_qrpassword = new JLabel("確認密碼"); 190 l_qrpassword.setBounds(20, 80, 100, 100); 191 pan.add(l_qrpassword); 192 l_qrpassword.setFont(f); 193 194 JPasswordField qrpassword = new JPasswordField(20); 195 password.setEchoChar('*'); 196 qrpassword.setBounds(100, 120, 150, 25); 197 pan.add(qrpassword); 198 199 200 201 JLabel l_sex = new JLabel("性別"); 202 pan.add(l_sex); 203 l_sex.setFont(f); 204 l_sex.setBounds(20, 140, 100, 100); 205 206 JPanel p1 = new JPanel(); //設定邊框 207 p1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); 208 p1.setBounds(100, 180, 150, 100); //設定邊框位置 209 210 JRadioButton male = new JRadioButton("男", true);// 單選框 211 JRadioButton female = new JRadioButton("女"); 212 ButtonGroup pan1=new ButtonGroup(); //設定單選框 213 pan1.add(male); 214 pan1.add(female); 215 pan.add(male); 216 pan.add(female); 217 male.setBounds(130,200,50,20); 218 female.setBounds(130,235,50,20); 219 pan.add(p1); 220 221 222 JLabel l_birthday = new JLabel("生日"); 223 l_birthday.setBounds(300, 0, 100, 100); 224 pan.add(l_birthday); 225 l_birthday.setFont(f); 226 // 日曆下拉選單 227 YearMonthDay testDate = new YearMonthDay(); 228 JPanel p = new JPanel(); 229 p.add(testDate.cboYear); 230 p.add(testDate.jLabel3); 231 p.add(testDate.cboMonth); 232 p.add(testDate.jLabel2); 233 p.add(testDate.cboDay); 234 p.add(testDate.jLabel1); 235 p.setBounds(345, 30, 205, 40); 236 pan.add(p); 237 238 239 JLabel l_hobby = new JLabel("興趣"); 240 l_hobby.setBounds(300, 50, 100, 100); 241 pan.add(l_hobby); 242 l_hobby.setFont(f); 243 JPanel jp2 = new JPanel(); 244 jp2.setBounds(350, 75, 240, 100); 245 jp2.setBorder(BorderFactory.createRaisedBevelBorder()); 246 pan.add(jp2); 247 248 JPanel l_x = new JPanel(); 249 JCheckBox[] interest = {new JCheckBox("閱讀",true),new JCheckBox("唱歌"),new JCheckBox("跳舞")}; 250 Box b2 = Box.createVerticalBox(); 251 b2.add(interest[0]); 252 b2.add(interest[1]); 253 b2.add(interest[2]); 254 jp2.add(l_x); 255 jp2.add(b2); 256 257 258 JLabel l_bz = new JLabel("備註"); 259 l_bz.setBounds(300, 140, 100, 100); 260 pan.add(l_bz); 261 l_bz.setFont(f); 262 JTextField tf_bz = new JTextField(100); 263 tf_bz.setBounds(350, 180, 180, 100); 264 pan.add(tf_bz); 265 266 267 // 新增按鈕 268 JButton b_register = new JButton("註冊"); 269 b_register.setBounds(250,500,100,50); 270 pan.add(b_register); 271 b_register.setFont(f); 272 JButton b_reset = new JButton("重置"); 273 b_reset.setBounds(450,500,100,50); 274 pan.add(b_reset); 275 b_reset.setFont(f); 276 277 } 278 279 }
執行結果圖:
問題:
性別單選框的外邊框,剛執行的時候沒有,放大窗口才會出現,這是為什麼?