Java實驗--基於Swing的簡單的歌曲信息管理系統(三)
轉載請註明出處,上一篇《Java實驗--基於Swing的簡單的歌曲信息管理系統(二)》介紹了項目的目錄結構和Dao層,本篇主要講解界面的繪制和業務層Service。
- 登錄界面
登錄界面設計,登錄界面窗體中有三個面板,一個主面板,兩個子面板,子面板放入主面板中。主面板采用網格布局(一行兩列),左邊的子面板采用流式布局,用來放圖片,右邊的子面板放入填入信息,最開始采用的是網格布局,但是效果不好,之後采用了絕對定位布局。如下:
同時我們為登錄按鈕和註冊按鈕添加監聽器和事件,當點擊登錄按鈕時,會對輸入的內容進行校驗,再調用service層的登錄方法,進行登錄,成功則根據用戶類型跳轉相應的主界面,當點擊註冊按鈕時,直接跳轉註冊頁面。這時我們就需要調用User的Service層了,service層再去調用dao層的相關方法即可。
布局代碼:
package shiyan6.view; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane;View Codeimport javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import shiyan6.entity.User; import shiyan6.service.UserService; import shiyan6.service.UserServiceImpl; public class LoginView extends JFrame { /** * */ private static final long serialVersionUID = 1L;private JPanel panel_main; // 主面板 private JPanel panel_left; // 左側面板 private JPanel panel_right; // 右側標簽 private JLabel lb_img; // 顯示圖片的標簽 private JLabel lb_uname; // 用戶標簽 private JLabel lb_upass; // 密碼標簽 private JLabel lb_type; // 登錄類型標簽 private JTextField tf_uname; // 用戶文本框 private JPasswordField pf_upass; // 密碼文本框 private JButton btn_login; // 登錄按鈕 private JButton btn_register; // 註冊按鈕 private JComboBox<String> cb_type; // 用戶角色下拉列表框 private UserService userService; private User user; public LoginView() { userService = new UserServiceImpl(); user = null; init(); login(); register(); } /** * 初始化控件 */ private void init() { this.setSize(600, 300); // 設置窗體大小 this.setLocationRelativeTo(null); // 設置窗體居中顯示 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 關閉按鈕 this.setTitle("登錄"); // 表題 this.setResizable(false); // 不可改變窗體大小 // 初始化面板 panel_main = new JPanel(new GridLayout(1, 2)); // 網格布局 panel_left = new JPanel(); // 流式布局 // panel_right = new JPanel(new GridLayout(4, 2, 0, 10)); // 效果不是很好 panel_right = new JPanel(null); // 使用絕對定位 // panel_right.setPreferredSize(new Dimension(300, 200)); // 初始化控件 lb_img = new JLabel(new ImageIcon(ClassLoader.getSystemResource("shiyan6/img/login.png"))); lb_uname = new JLabel("用戶:", JLabel.CENTER); // 居中顯示 lb_uname.setBounds(20, 20, 80, 26); lb_upass = new JLabel("密碼:", JLabel.CENTER); lb_upass.setBounds(20, 70, 80, 26); lb_type = new JLabel("類型", JLabel.CENTER); lb_type.setBounds(20, 120, 80, 26); tf_uname = new JTextField(8); tf_uname.setBounds(80, 20, 180, 30); pf_upass = new JPasswordField(8); pf_upass.setBounds(80, 70, 180, 30); cb_type = new JComboBox<String>(new String[] { "普通用戶", "管理員" }); cb_type.setBounds(80, 120, 100, 30); btn_login = new JButton("登錄"); btn_login.setBounds(30, 170, 80, 26); btn_register = new JButton("註冊"); btn_register.setBounds(180, 170, 80, 26); // 把相應的控件放到面板中去 panel_left.add(lb_img); panel_right.add(lb_uname); panel_right.add(tf_uname); panel_right.add(lb_upass); panel_right.add(pf_upass); panel_right.add(lb_type); panel_right.add(cb_type); panel_right.add(btn_login); panel_right.add(btn_register); // 主面板中放左右兩個面板 panel_main.add(panel_left); panel_main.add(panel_right); // 再把主面板放到窗體中 this.getContentPane().add(panel_main); //this.pack(); // 收縮一下 this.setVisible(true); // 顯示窗體 } /** * 用戶登錄 */ private void login() { btn_login.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 獲取用戶名和密碼,類別加1 String uname = tf_uname.getText().trim(); String upass = new String(pf_upass.getPassword()); int type = cb_type.getSelectedIndex() + 1; if (uname.equals("")) { JOptionPane.showMessageDialog(LoginView.this, "用戶名不能為空"); } else if (upass.equals("")) { JOptionPane.showMessageDialog(LoginView.this, "密碼不能為空"); } user = userService.login(uname, upass); System.out.println(user); System.out.println(user != null); if (null != user) { if (type != user.getRole()) { JOptionPane.showMessageDialog(LoginView.this, "身份類型錯誤,請重新選擇"); } else { if (type == 1) { // 普通用戶 // 傳遞過去的user用於顯示信息 new UserMainView(user); LoginView.this.dispose(); // 關閉登陸框 } else { // 管理員 // 傳遞過去的user用於顯示信息 new AdminMainView(user); LoginView.this.dispose(); // 關閉登陸框 } } } else { JOptionPane.showMessageDialog(LoginView.this, "用戶或密碼錯誤"); } } }); } /** * 註冊 */ private void register() { btn_register.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new RegisterView(); } }); } }
UserService接口部分代碼:
package shiyan6.service; import java.util.List; import shiyan6.entity.User; /** * User的業務層 * */ public interface UserService { /** * 用戶登錄 * @param name,登錄名 * @param password * @return */ User login(String name, String password); /** * 用戶註冊 * @param user * @return */ boolean register(User user); /** * 查看用戶是否存在 * @param name * @return */ boolean checkUser(String name); }View Code
UserServiceImple實現類部分代碼:
package shiyan6.service; import java.util.List; import shiyan6.dao.UserDao; import shiyan6.dao.UserDaoImpl; import shiyan6.entity.User; public class UserServiceImpl implements UserService { UserDao userDao = new UserDaoImpl(); public User login(String name, String password) { return userDao.findByNameAndPass(name, password); } @Override public boolean register(User user) { return userDao.addUser(user); } @Override public boolean checkUser(String name) { int count = userDao.findCountByName(name); System.out.println("conutservice:"+count); if (count == 0) { return false; } else { return true; } } }View Code
效果如下:
- 註冊界面
同理登錄頁面一樣,開始采用的時網格布局,但是效果極差,後面采用的是絕對定位。
同樣需要為取消按鈕和註冊按鈕註冊監聽器、添加事件。
代碼如下:
package shiyan6.view; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import shiyan6.entity.User; import shiyan6.service.UserService; import shiyan6.service.UserServiceImpl; import shiyan6.util.Common; public class RegisterView extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel panelMain; // 主面板 private JLabel labName; // 用戶名 private JLabel labPass; // 密碼 private JLabel labConfirmpass; // 確認新密碼 private JTextField tfName; // 用戶名 private JPasswordField pfPass; // 舊密碼輸入框 private JPasswordField pfConfirmpass; // 確認密碼輸入 private JButton btnConcel; // 取消按鈕 private JButton btnRegsit; // 修改按鈕 private UserService userService; public RegisterView() { userService = new UserServiceImpl(); init(); cancel(); register(); } /** * 初始化各組件 */ private void init() { this.setTitle("註冊信息"); this.setSize(350, 300); this.setLocationRelativeTo(null); // 設置窗體居中顯示 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 關閉按鈕 // panelMain = new JPanel(new GridLayout(5, 1)); // 5行1列,效果極差 panelMain = new JPanel(null); // 使用絕對布局 labName = new JLabel("名字:"); labName.setFont(new Font("宋體", Font.BOLD, 15)); // 設置字體 labName.setBounds(40, 20, 80, 26); tfName = new JTextField(); tfName.setBounds(150, 20, 100, 26); panelMain.add(labName); panelMain.add(tfName); labPass = new JLabel("密碼:"); labPass.setFont(new Font("宋體", Font.BOLD, 15)); // 設置字體 labPass.setBounds(40, 70, 80, 26); pfPass = new JPasswordField(); pfPass.setBounds(150, 70, 100, 26); panelMain.add(labPass); panelMain.add(pfPass); labConfirmpass = new JLabel("再次確認:"); labConfirmpass.setFont(new Font("宋體", Font.BOLD, 15)); // 設置字體 labConfirmpass.setBounds(40, 120, 80, 26); pfConfirmpass = new JPasswordField(); pfConfirmpass.setBounds(150, 120, 100, 26); panelMain.add(labConfirmpass); panelMain.add(pfConfirmpass); btnConcel = new JButton("取消"); btnConcel.setBounds(60, 170, 60, 30); btnRegsit = new JButton("註冊"); btnRegsit.setBounds(170, 170, 60, 30); panelMain.add(btnConcel); panelMain.add(btnRegsit); this.getContentPane().add(panelMain); this.setVisible(true); } /** * 取消按鈕,將文本框的類容給清空 */ private void cancel() { btnConcel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { tfName.setText(""); pfPass.setText(""); pfConfirmpass.setText(""); } }); } /** * 用戶註冊 */ private void register() { tfName.addFocusListener(new FocusAdapter() { public void focusLost(FocusEvent e) { String name = tfName.getText().trim(); // 查看用戶名是否存在 if (userService.checkUser(name)) { JOptionPane.showMessageDialog(panelMain, "該用戶名已被註冊", "消息提示框", JOptionPane.INFORMATION_MESSAGE); return; } } }); btnRegsit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String name = tfName.getText().trim(); String password = new String(pfPass.getPassword()); String compassword = new String(pfConfirmpass.getPassword()); if (name.equals("") || name == null || password.equals("") || password == null || compassword.equals("") || compassword == null) { JOptionPane.showMessageDialog(panelMain, "填入信息不能有空", "消息提示框", JOptionPane.INFORMATION_MESSAGE); return; } if (!password.equals(compassword)) { JOptionPane.showMessageDialog(panelMain, "兩次密碼不一致", "消息提示框", JOptionPane.INFORMATION_MESSAGE); return; } User user = new User(Common.getUuid(), name, password, 1); System.out.println(user); // 把用戶信息給添加到數據庫中 if (userService.register(user)) { JOptionPane.showMessageDialog(panelMain, "註冊成功", "消息提示框", JOptionPane.INFORMATION_MESSAGE); RegisterView.this.dispose(); new LoginView(); } else { JOptionPane.showMessageDialog(panelMain, "註冊失敗", "消息提示框", JOptionPane.INFORMATION_MESSAGE); return; } } }); } }View Code
效果如下:
- 管理員主界面
用戶管理主界面窗體中,包含一個主面板,一個歡迎信息的子面板,一個顯示功能按鈕鍵的子面板,一個顯示圖片和以後窗體的子面板(不過這裏使用的是JDesktopPane,它可以顯示並管理眾多JInternalFrame)。
主面板采用了邊界布局,通過設計圖知道,顯示歡迎信息的子面板位於主面板的上邊(NORTH),顯示功能按鈕的子面板位於主面板的左邊(WEST),顯示圖片和以後窗體的子面板位於主面板上的中間(CENTER)。
這裏唯一的難點就是滾動的顯示用戶歡迎信息,需要開一個子線程來做,因為Swing是單線程的。還要為幾個按鈕給註冊監聽器和添加點擊事件。布局代碼如下:
package shiyan6.view; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import shiyan6.entity.User; /** * 管理園的主窗體 * */ public class AdminMainView extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel panelMain; // 主面板 private JPanel panelTop; // 主面板的上部 private JLabel labWelcome; // 上部的歡迎信息 private JPanel panelLeft; // 主面板的左邊 private JButton btnSongManage; // 歌曲管理按鈕 private JButton btnUserManage; // 用戶管理按鈕 private JButton btnEditUserInfo; // 修改密碼按鈕 private JDesktopPane panelContent; // 用來顯示類容 private JLabel labImg; // 用來存放圖片 private User user; public AdminMainView(User user) { this.user = user; init(); songManageView(); adminUserManageView(); userInfoEditView(); } /** * 初始化組件信息 */ private void init() { panelMain = new JPanel(new BorderLayout()); panelTop = new JPanel(); labWelcome = new JLabel("歡 迎 管 理 員 "+user.getName()+" 進 入 歌 曲 管 理 系 統"); labWelcome.setFont(new Font("宋體", Font.BOLD, 22));// 設置字體 labWelcome.setForeground(Color.BLUE); // 設置顏色 panelTop.add(labWelcome); // panelTop.setSize(1000, 200); // 設置大小 panelTop.setPreferredSize(new Dimension(1000, 100)); // EventQueue事件,讓labWelcome給動起來 EventQueue.invokeLater(new Runnable() { public void run() { new Thread(new DynaminThread()).start(); } }); panelMain.add(panelTop, BorderLayout.NORTH); // 添加到主面板的上方 panelLeft = new JPanel(new GridLayout(9, 1, 0, 40)); panelLeft.setBorder(BorderFactory.createTitledBorder("菜單欄")); btnSongManage = new JButton("歌曲管理"); btnUserManage = new JButton("用戶管理"); btnEditUserInfo = new JButton("修改密碼"); panelLeft.add(new JLabel()); //填充 panelLeft.add(btnSongManage); panelLeft.add(new JLabel()); panelLeft.add(btnUserManage); panelLeft.add(new JLabel()); panelLeft.add(btnEditUserInfo); // panelLeft.setSize(400, 600); // 設置大小,此方法無效 panelLeft.setPreferredSize(new Dimension(200, 600)); panelMain.add(panelLeft, BorderLayout.WEST); // 添加到主面板的左邊 panelContent = new JDesktopPane(); ImageIcon image = new ImageIcon("src/shiyan6/img/song.jpg"); // System.out.println(image); labImg = new JLabel(image); labImg.setBounds(15, 15, 750, 550); // 設置位置和大小 panelContent.setPreferredSize(new Dimension(800, 600)); panelContent.add(labImg, new Integer(Integer.MIN_VALUE)); // 將存放圖片的label放在最下層 panelContent.setBorder(BorderFactory.createTitledBorder("內容")); panelMain.add(panelContent, BorderLayout.CENTER); // 添加到主界面的中間 this.setTitle("歌曲管理系統"); // 窗體標題 this.getContentPane().add(panelMain); // 將主面板給加入到窗體中 this.setSize(1000, 800); // 設置窗體大小 this.setLocationRelativeTo(null); // 讓窗體顯示在屏幕中央 this.setResizable(false); // 窗體大小不可變 this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 設置關閉按鈕 this.setBackground(Color.white); this.setVisible(true); // 讓窗體可見 } /** * 讓歡迎的label動起來, * 因為swing是單線程的,因此需要啟動一個線程 * */ private class DynaminThread implements Runnable { public void run() { while(true) { for(int i = 1000; i > -980; i --) { try { Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } labWelcome.setLocation(i, 30); } } } } /** * 添加事件,跳轉管理歌曲的頁面 */ private void songManageView() { btnSongManage.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // System.out.println(e.getActionCommand()); AdminSongManageView sManagerView = new AdminSongManageView(); // 將指定的視圖給添加到JDeskTopPanel中 panelContent.add(sManagerView); // 將視圖放在最前面 sManagerView.toFront(); } }); } /** * 用戶管理界面 */ private void adminUserManageView() { btnUserManage.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AdminUserManageView adminUserManageView = new AdminUserManageView(); // 將指定的視圖給添加到JDeskTopPanel中 panelContent.add(adminUserManageView); // 將視圖放在最前面 adminUserManageView.toFront(); } }); } /** * 進入密碼修改界面 */ private void userInfoEditView() { btnEditUserInfo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { UserInfoEditView userInfoEditView = new UserInfoEditView(user); // 將指定的視圖給添加到JDeskTopPanel中 panelContent.add(userInfoEditView); // 將視圖給放在最前面 userInfoEditView.toFront(); } }); } }View Code
效果如下:
項目源碼
Java實驗--基於Swing的簡單的歌曲信息管理系統(三)