练习~图书管理系统
Posted 枫林晚月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习~图书管理系统相关的知识,希望对你有一定的参考价值。
早期习作~~GUI的练习
采用卡片式布局,每次访问卡片前都会判断页面是否已经被内存回收。
一、架构
二、界面
三、源码
UI层:
1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class libUI{ 6 //主方法 7 public static void main(String [] args){ 8 libUI ui = new libUI(); 9 ui.frame(); 10 ui.initBO(); 11 ui.login(); 12 } 13 14 public libBO bo = null; 15 public void initBO(){ 16 bo = new libBO(); 17 } 18 19 //界面大小 20 public int w = 820; 21 public int h = 620; 22 23 public JFrame jf = null; 24 public JLayeredPane layeredp = null; 25 public JLayeredPane backlayer = null; 26 public JPanel picp = null; 27 public JPanel title = null; 28 public JLabel subhead = null; 29 public JPanel frontlayer = null; 30 public CardLayout cardl = new CardLayout(0, 0); 31 32 //窗口及基本图层 33 public void frame(){ 34 layeredp = new JLayeredPane(); 35 layeredp.setPreferredSize(new Dimension(w, h)); 36 37 //窗口 38 jf = new JFrame("图书管理系统"); 39 jf.add(layeredp); 40 jf.setResizable(false); 41 jf.pack(); 42 jf.setVisible(true); 43 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 44 45 //背景层 46 backlayer = new JLayeredPane(); 47 backlayer.setSize(w, h); 48 backlayer.setLocation(0, 0); 49 //载入图片 50 picp = new JPanel(); 51 picp.setLayout(cardl); 52 JLabel bj1 = new JLabel(new ImageIcon("res/1.jpg")); 53 picp.add("p1", bj1); 54 JLabel bj2 = new JLabel(new ImageIcon("res/2.jpg")); 55 picp.add("p2", bj2); 56 JLabel bj3 = new JLabel(new ImageIcon("res/3.jpg")); 57 picp.add("p3", bj3); 58 JLabel bj4 = new JLabel(new ImageIcon("res/4.jpg")); 59 picp.add("p4", bj4); 60 picp.setSize(w, h); 61 picp.setLocation(0, 0); 62 backlayer.add(picp, new Integer(0)); 63 picp.validate(); 64 //标题 65 title = new JPanel(); 66 title.setOpaque(false); 67 Box titlebox = Box.createVerticalBox(); 68 69 titlebox.add(Box.createVerticalStrut(80)); 70 71 JPanel tittipp = new JPanel(); 72 tittipp.setOpaque(false); 73 JLabel tittips = new JLabel(new ImageIcon("res/标题-1.png")); 74 tittipp.add(tittips); 75 titlebox.add(tittipp); 76 77 JPanel subheadp = new JPanel(); 78 subheadp.setOpaque(false); 79 subhead = new JLabel(); 80 subhead.setFont(new Font("华文行楷", Font.PLAIN, 30)); 81 subheadp.add(subhead); 82 titlebox.add(subheadp); 83 title.add(titlebox); 84 title.setLocation(0, 0); 85 title.setSize(w, h); 86 backlayer.add(title, new Integer(1)); 87 88 layeredp.add(backlayer, new Integer(0)); 89 90 //前景层 91 frontlayer = new JPanel(); 92 frontlayer.setLayout(cardl); 93 frontlayer.setOpaque(false); 94 frontlayer.setSize(w, h); 95 frontlayer.setLocation(0, 0); 96 97 layeredp.add(frontlayer, new Integer(1)); 98 } 99 100 //登录层 101 public JPanel loginp = null; 102 public JPasswordField logpi = null; 103 public JLabel logt = null; 104 public void login(){ 105 if(loginp == null){ 106 //界面 107 loginp = new JPanel(); 108 loginp.setLayout(null); 109 loginp.setOpaque(false); 110 111 Box loginbox = Box.createVerticalBox(); 112 loginbox.setLocation(10, 380); 113 loginbox.setSize(400, 180); 114 loginp.add(loginbox); 115 116 JPanel denglup = new JPanel(); 117 denglup.setOpaque(false); 118 JLabel denglu = new JLabel("管理员登陆"); 119 denglu.setForeground(new Color(20, 150, 250)); 120 denglu.setFont(new Font("华文仿宋", Font.BOLD, 25)); 121 denglup.add(denglu); 122 loginbox.add(denglup); 123 124 JPanel logpp = new JPanel(); 125 logpp.setOpaque(false); 126 JLabel logpt = new JLabel("登陆密码:"); 127 logpt.setForeground(new Color(20, 150, 250)); 128 logpt.setFont(new Font("华文中宋", Font.PLAIN, 20)); 129 logpi = new JPasswordField(8); 130 logpi.setForeground(Color.BLUE); 131 logpi.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 132 logpp.add(logpt); 133 logpp.add(logpi); 134 loginbox.add(logpp); 135 136 JPanel logbp =new JPanel(); 137 logbp.setOpaque(false); 138 JButton logb = new JButton("登 录"); 139 logb.setFont(new Font("微软雅黑", Font.PLAIN, 15)); 140 logbp.add(logb); 141 loginbox.add(logbp); 142 143 JPanel logtp = new JPanel(); 144 logtp.setOpaque(false); 145 logt = new JLabel(" "); 146 logt.setForeground(new Color(238,22,22)); 147 logt.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 148 logtp.add(logt); 149 loginbox.add(logtp); 150 151 //显示 152 frontlayer.add("login", loginp); 153 cardl.show(picp, "p1"); 154 cardl.show(frontlayer, "login"); 155 frontlayer.validate(); 156 logpi.requestFocus(); 157 158 //监听 159 logb.addActionListener(new ActionListener(){ 160 public void actionPerformed(ActionEvent ae){ 161 char[] passi = logpi.getPassword(); 162 if(passi.length == 0){ 163 logt.setText("密码不能为空!"); 164 }else{ 165 try { 166 int n = bo.dologin(passi); 167 if(n == 0){ 168 logt.setText("密码错误!"); 169 logpi.setText(""); 170 }else{ 171 logt.setText(" "); 172 logpi.setText(""); 173 menu(); 174 } 175 } catch (Exception e) { 176 logt.setText("出错了!"); 177 } 178 } 179 } 180 }); 181 182 }else{ 183 cardl.show(frontlayer, "login"); 184 cardl.show(picp, "p1"); 185 logpi.requestFocus(); 186 } 187 188 } 189 190 //主菜单界面 191 public JPanel menup = null; 192 public void menu(){ 193 if(menup == null){ 194 menup = new JPanel(); 195 menup.setOpaque(false); 196 menup.setLayout(new BorderLayout()); 197 198 //左边按钮 199 Box westbox = Box.createVerticalBox(); 200 menup.add(westbox, BorderLayout.WEST); 201 202 westbox.add(Box.createVerticalStrut(220)); 203 204 JButton addb = new JButton("新增图书"); 205 addb.setFont(new Font("华文中宋", Font.PLAIN, 25)); 206 westbox.add(addb); 207 208 westbox.add(Box.createVerticalStrut(80)); 209 210 JButton lendb = new JButton("借书"); 211 lendb.setFont(new Font("华文中宋", Font.PLAIN, 25)); 212 westbox.add(lendb); 213 214 westbox.add(Box.createVerticalStrut(80)); 215 216 JButton returnb = new JButton("还书"); 217 returnb.setFont(new Font("华文中宋", Font.PLAIN, 25)); 218 westbox.add(returnb); 219 220 //右边按钮 221 Box eastbox = Box.createVerticalBox(); 222 menup.add(eastbox, BorderLayout.EAST); 223 224 eastbox.add(Box.createVerticalStrut(200)); 225 226 JButton bookb = new JButton("<html>图书信息查询<br>【修改/删除】</html>"); 227 bookb.setFont(new Font("华文中宋", Font.PLAIN, 20)); 228 bookb.setAlignmentX(Component.RIGHT_ALIGNMENT); 229 eastbox.add(bookb); 230 231 eastbox.add(Box.createVerticalStrut(70)); 232 233 JButton inquiryb = new JButton("<html>学员借书<br>情况查询</html>"); 234 inquiryb.setFont(new Font("华文中宋", Font.PLAIN, 22)); 235 inquiryb.setAlignmentX(Component.RIGHT_ALIGNMENT); 236 eastbox.add(inquiryb); 237 238 eastbox.add(Box.createVerticalStrut(70)); 239 240 JButton quitb = new JButton("退出"); 241 quitb.setFont(new Font("华文中宋", Font.PLAIN, 25)); 242 quitb.setAlignmentX(Component.RIGHT_ALIGNMENT); 243 eastbox.add(quitb); 244 245 //显示 246 frontlayer.add("menu", menup); 247 cardl.show(picp, "p2"); 248 cardl.show(frontlayer, "menu"); 249 frontlayer.validate(); 250 251 //监听 252 addb.addActionListener(new ActionListener(){ 253 public void actionPerformed(ActionEvent ae){ 254 add(); 255 } 256 }); 257 258 lendb.addActionListener(new ActionListener(){ 259 public void actionPerformed(ActionEvent ae){ 260 lend(); 261 } 262 }); 263 264 returnb.addActionListener(new ActionListener(){ 265 public void actionPerformed(ActionEvent ae){ 266 returnbook(); 267 } 268 }); 269 270 bookb.addActionListener(new ActionListener(){ 271 public void actionPerformed(ActionEvent ae){ 272 book(); 273 } 274 }); 275 276 inquiryb.addActionListener(new ActionListener(){ 277 public void actionPerformed(ActionEvent ae){ 278 inquiry(); 279 } 280 }); 281 282 quitb.addActionListener(new ActionListener(){ 283 public void actionPerformed(ActionEvent ae){ 284 quit(); 285 } 286 }); 287 }else{ 288 initBO(); 289 cardl.show(picp, "p2"); 290 subhead.setText(""); 291 cardl.show(frontlayer, "menu"); 292 } 293 } 294 295 //成功提示界面 296 public JPanel suctipsp = null; 297 public JLabel suctipsl = null; 298 public JButton suctipsb = null; 299 public void suctips(){ 300 if(suctipsp == null){ 301 suctipsp = new JPanel(); 302 suctipsp.setOpaque(false); 303 Box suctipsbox = Box.createVerticalBox(); 304 suctipsp.add(suctipsbox); 305 306 suctipsbox.add(Box.createVerticalStrut(230)); 307 308 JPanel suctipslp = new JPanel(); 309 suctipslp.setOpaque(false); 310 suctipsl = new JLabel(subhead.getText() + "成功!"); 311 suctipsl.setFont(new Font("华文中宋", Font.PLAIN, 25)); 312 suctipslp.add(suctipsl); 313 suctipsbox.add(suctipslp); 314 315 suctipsbox.add(Box.createVerticalStrut(30)); 316 317 JPanel suctipsbp = new JPanel(); 318 suctipsbp.setOpaque(false); 319 suctipsb = new JButton("返 回"); 320 suctipsb.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 321 suctipsbp.add(suctipsb); 322 suctipsbox.add(suctipsbp); 323 324 //显示 325 frontlayer.add("suctips", suctipsp); 326 cardl.show(picp, "p4"); 327 cardl.show(frontlayer, "suctips"); 328 frontlayer.validate(); 329 330 //监听 331 suctipsb.addActionListener(new ActionListener(){ 332 public void actionPerformed(ActionEvent ae){ 333 menu(); 334 } 335 }); 336 }else{ 337 frontlayer.add("suctips", suctipsp); 338 cardl.show(picp, "p4"); 339 cardl.show(frontlayer, "suctips"); 340 suctipsl.setText(subhead.getText() + "成功!"); 341 } 342 } 343 344 //新增图书 345 public JPanel addp = null; 346 public JTextField addsi = null; 347 public JTextField addzi = null; 348 public JTextField addci = null; 349 public JLabel addt = null; 350 public void add(){ 351 if(addp == null){ 352 //界面 353 addp = new JPanel(); 354 addp.setOpaque(false); 355 Box addbox = Box.createVerticalBox(); 356 addp.add(addbox); 357 358 addbox.add(Box.createVerticalStrut(250)); 359 360 JPanel addsp = new JPanel(); 361 addsp.setOpaque(false); 362 JLabel addst = new JLabel(" 书 名 :"); 363 addst.setFont(new Font("华文中宋", Font.PLAIN, 20)); 364 addsi = new JTextField(8); 365 addsi.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 366 addsp.add(addst); 367 addsp.add(addsi); 368 addbox.add(addsp); 369 370 JPanel addzp = new JPanel(); 371 addzp.setOpaque(false); 372 JLabel addzt = new JLabel(" 作 者 :"); 373 addzt.setFont(new Font("华文中宋", Font.PLAIN, 20)); 374 addzi = new JTextField(8); 375 addzi.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 376 addzp.add(addzt); 377 addzp.add(addzi); 378 addbox.add(addzp); 379 380 JPanel addcp = new JPanel(); 381 addcp.setOpaque(false); 382 JLabel addct = new JLabel("出版社:"); 383 addct.setFont(new Font("华文中宋", Font.PLAIN, 20)); 384 addci = new JTextField(8); 385 addci.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 386 addcp.add(addct); 387 addcp.add(addci); 388 addbox.add(addcp); 389 390 addbox.add(Box.createVerticalStrut(30)); 391 392 JPanel addbp =new JPanel(); 393 addbp.setOpaque(false); 394 JButton addqb = new JButton("确 认"); 395 addqb.setFont(new Font("微软雅黑", Font.PLAIN, 15)); 396 addbp.add(addqb); 397 JLabel addbt = new JLabel(" "); 398 addbt.setFont(new Font("华文中宋", Font.PLAIN, 15)); 399 addbp.add(addbt); 400 JButton addfb = new JButton("返 回"); 401 addfb.setFont(new Font("微软雅黑", Font.PLAIN, 15)); 402 addbp.add(addfb); 403 addbox.add(addbp); 404 405 addbox.add(Box.createVerticalStrut(20)); 406 407 JPanel addtp = new JPanel(); 408 addtp.setOpaque(false); 409 addt = new JLabel(); 410 addt.setForeground(Color.red); 411 addt.setFont(new Font("微软雅黑", Font.PLAIN, 18)); 412 addtp.add(addt); Java 基础语法纯 Java《图书管理系统》练习