1.项目布局(供新手参考)
学校老师任务,因为我也是新手所以写的不是特别的好,所以可以提供参考。
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
//工具类
public class Head {
public static JFrame frame = new JFrame("登录");
public static final int width = 900;
public static final int height = 600;
public static String UserName=null;
//动态切换面板
//获取屏幕分辨率 设置窗体在屏幕居中的位置
public static void initFrame() {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取一个与系统相关的工具类对象
Dimension size = toolkit.getScreenSize();//获取屏幕分辨率
int x = (int) size.getWidth();//获取中心位置的宽
int y = (int) size.getHeight();//获取中心位置的高
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);//设置窗口位置,大小
frame.setVisible(true);//设置窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
}
public static void initFrame(JFrame frame) {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取一个与系统相关的工具类对象
Dimension size = toolkit.getScreenSize();//获取屏幕分辨率
int x = (int) size.getWidth();//获取中心位置的宽
int y = (int) size.getHeight();//获取中心位置的高
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);//设置窗口位置,大小
frame.setVisible(true);//设置窗口可见
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
}
//设置子窗口 获取屏幕分辨率 设置窗体在屏幕居中的位置
public static void initJDialog(JDialog frame) {
Toolkit toolkit = Toolkit.getDefaultToolkit();//获取一个与系统相关的工具类对象
Dimension size = toolkit.getScreenSize();//获取屏幕分辨率
int x = (int) size.getWidth();//获取中心位置的宽
int y = (int) size.getHeight();//获取中心位置的高
frame.setBounds((x - width) / 2, (y - height) / 2, width, height);//设置窗口位置,大小
frame.setVisible(true);//设置窗口可见
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭
}
//修改用户密码页面
public static void Update() {
UpdatePasswd.Update(frame);
}
}
//测试运行代码块
package GUI_Object;
import GUI_Object.GUI.Head;
import GUI_Object.GUI.Login;
public class dome {
public static void main(String[] args) {
Login.LoginWind();
Head.initFrame();
}
}
2.登录注册页面
登录页面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Login {
private static Boolean Judge = true;
//账户密码登录窗口loginWind
public static void LoginWind() {
JPanel panel = new JPanel();
Head.frame.setBackground(new Color(0xd4f6ff));
panel.setOpaque(false);
panel.setLayout(new FlowLayout());
panel.setLayout(null);
// 设置背景图片
//添加标签【学生管理系统】
ImageIcon imageIcon=new ImageIcon("src/GUI_Object/images/logo.jpg");
JLabel textStudentManage = new JLabel(imageIcon);//创建一个标签,并命名为“学生管理系统“
textStudentManage.setForeground(new Color(0XFFAEB9));//设置字体颜色
textStudentManage.setFont(new Font("黑体", Font.PLAIN, 50));//设置字体大小
textStudentManage.setBounds(0, 10, 900, 100);//设置标签的绝对位置
panel.add(textStudentManage);//向框架中添加组件【标签(学生管理系统)】
//添加标签【用户名】
JLabel textUser = new JLabel("用户名:");
textUser.setForeground(new Color(0Xdbb4f7));
textUser.setFont(new Font("黑体", Font.PLAIN, 30));
textUser.setBounds(200, 140, 200, 100);
panel.add(textUser);
//添加输入框【用户名输入框】
JTextField user = new JTextField(20);
user.setFont(new Font("黑体", Font.PLAIN, 18));
user.setSelectedTextColor(new Color(0xFFF5EE));
user.setBounds(330, 170, 280, 40);
panel.add(user);
//添加标签【密码】
JLabel textPassword = new JLabel("密码 :");
textPassword.setForeground(new Color(0Xdbb4f7));
textPassword.setFont(new Font("黑体", Font.PLAIN, 30));
textPassword.setBounds(200, 200, 200, 100);
panel.add(textPassword);
//添加密码输入框【密码】
JPasswordField password = new JPasswordField(20);
password.setBounds(330, 230, 280, 40);
panel.add(password);
//添加按钮【登录】
JButton jButton = new JButton("登录");
jButton.setForeground(new Color(0xffcbe1));
jButton.setBackground(new Color(0xfffde4));
jButton.setFont(new Font("黑体", Font.PLAIN, 20));
jButton.setBorderPainted(false);
jButton.setBounds(300, 330, 100, 50);
panel.add(jButton);
//添加按钮【注册】
JButton register = new JButton("注册");
register.setForeground(new Color(0xffcbe1));
register.setBackground(new Color(0xfffde4));
register.setFont(new Font("黑体", Font.PLAIN, 20));
register.setBorderPainted(false);
register.setBounds(500, 330, 100, 50);
panel.add(register);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = user.getText();//获取账户
String passwd = password.getText();//获取密码
int i = Database.QueryUser(name, passwd);
switch (i) {
case 0:
JOptionPane.showMessageDialog(panel, "登录成功", "通知", JOptionPane.INFORMATION_MESSAGE);
AverageUser.Subpage();
Head.UserName=name;
break;//普通用户登录页面
case 1:
JOptionPane.showMessageDialog(panel, "登录成功", "通知", JOptionPane.INFORMATION_MESSAGE);
Admin.Subpage();
SwingUtilities.updateComponentTreeUI(Head.frame);
Head.UserName=name;
break;//管理员用户登录页面
default:
JOptionPane.showMessageDialog(panel, "警告密码或者账户输入错误", "警告", JOptionPane.WARNING_MESSAGE);
}
}
});
// JButton button1 = new JButton("注册");
// panel.add(button1);
register.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//创建页面2;
if (Judge) {
Database.CreateAccount();
Judge = false;
}
GUI_Object.GUI.register.Subpage(Head.frame);
}
});
Head.frame.setContentPane(panel);
}
}
注册页面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* 注册页面
* */
public class register {
//创建子页面
public static void Subpage(JFrame jFrame){
JDialog jDialog = new JDialog(jFrame,"注册页面",true);//设置由谁弹出,名字,鼠标选中对话框时不能对其他对话框改变
jDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
panel.setLayout(null);
panel.setBackground(new Color(0xe7fafe));
ImageIcon icon =new ImageIcon("src/GUI_Object/images/RegisterLogo.jpg");
JLabel textStudentManage = new JLabel(icon);//创建一个标签,并命名为“学生管理系统“
textStudentManage.setBounds(0, 0, 900, 90);//设置标签的绝对位置
panel.add(textStudentManage);
//用户名
JLabel name = new JLabel("用户名:");
name.setForeground(new Color(0xa5d478));
name.setFont(new Font("黑体", Font.PLAIN, 30));
name.setBounds(200, 140, 200, 100);
panel.add(name);
//用户输入框
JTextField NameField = new JTextField(12);
NameField.setFont(new Font("黑体", Font.PLAIN, 18));
// NameField.setSelectedTextColor(new Color(0xFF0000));
NameField.setBounds(330, 170, 280, 40);
panel.add(NameField);
//标签密码
JLabel passwd = new JLabel("密码:");
passwd.setForeground(new Color(0xa5d478));
passwd.setFont(new Font("黑体", Font.PLAIN, 30));
passwd.setBounds(200, 200, 200, 100);
panel.add(passwd);
//密码框
JPasswordField passwordField= new JPasswordField(12);
passwordField.setBounds(330, 230, 280, 40);
panel.add(passwordField);
//下拉列表
Object[] arr = {"普通用户","管理员用户"};
JLabel label = new JLabel("用户权限:");
label.setForeground(new Color(0xa5d478));
label.setFont(new Font("黑体", Font.PLAIN, 30));
label.setBounds(200, 260, 200, 100);
JComboBox jComboBox = new JComboBox(arr);
jComboBox.setBounds(340, 290, 80, 40);
panel.add(label);
panel.add(jComboBox);
//确认按钮
JButton register = new JButton("确认");
register.setForeground(new Color(0xa5d478));
register.setBackground(new Color(0xfffed5));
register.setFont(new Font("黑体", Font.PLAIN, 20));
register.setBorderPainted(false);
register.setBounds(430, 290, 80, 40);
panel.add(register);
// JButton button = new JButton("确认");
register.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int i=10;
i = JOptionPane.showConfirmDialog(panel, "您确认注册用户吗?");
if (i==0) {
String text = NameField.getText();
String password = passwordField.getText();
int selectedIndex = jComboBox.getSelectedIndex();//获取选到的下拉向
Boolean aBoolean = Database.AddUser(text, password, selectedIndex);
if (aBoolean) {
JOptionPane.showMessageDialog(panel, "恭喜你,用户注册成功", "通知", JOptionPane.INFORMATION_MESSAGE);
jDialog.setVisible(false);
jFrame.setVisible(true);
} else {
JOptionPane.showMessageDialog(panel, "抱歉,用户注册失败", "警告", JOptionPane.ERROR_MESSAGE);
}
}
}
});
//返回键
JButton register1 = new JButton("返回");
register1.setForeground(new Color(0xfddfeb));
register1.setBackground(new Color(0x84d696));
register1.setFont(new Font("黑体", Font.PLAIN, 20));
register1.setBorderPainted(false);
register1.setBounds(0, 0, 80, 40);
register1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
jDialog.setVisible(false);
jFrame.setVisible(true);
}
});
panel.add(register1);
jDialog.add(panel);//新页面
jFrame.setVisible(false);
Head.initJDialog(jDialog);
}
}
3. 管理员页面
以子窗口的形式,实现用户,图书的增删改查(删改图书查询没有用到like的模糊查询,有需要的可以增做修改)
管理员登录界面
package GUI_Object.GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* 管理员登录页面
* */
public class Admin {
public static void Subpage(){
JPanel jPanel = new JPanel(new BorderLayout());
JMenuBar menuBar = new JMenuBar();//创建菜单栏的容器
menuBar.setSize(900,60);
menuBar.setFont(new Font("黑体", Font.PLAIN, 60));
menuBar.setBounds(0,0,900,60);
JMenu menu = new JMenu("用户管理");
JMenu menu1 = new JMenu("图书管理");
menuBar.add(menu);
menuBar.add(menu1);
JMenuItem item =new JMenuItem("添加修改用户密码");
JMenuItem item2 = new JMenuItem("退出登录");
JMenuItem item3 = new JMenuItem("添加图书");
JMenuItem item4 = new JMenuItem("删除图书");
item.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Head.Update();
}
});
item2.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Login.LoginWind();
SwingUtilities.updateComponentTreeUI(Head.frame);
}
});
menu.add(item);
menu.add(item2);
menu1.add(item3);
menu1.add(item4);
//添加图书
item3.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
LibraryManager.Library();
}
});
//删除图书
item4.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
DropBook.Wind();
}
});
item2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
}
});
jPanel.add(menuBar,BorderLayout.NORTH);
Head.frame.setContentPane(jPanel);
}
}
用户管理子界面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import GUI_Object.opjo.User;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.xml.crypto.Data;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
//管理员页面子页面修改添加用户
public class UpdatePasswd {
public static JDesktopPane jDesktopPane = new JDesktopPane();//主页面 主面板
private static String[] columnNames = new String[]{"用户名", "密码", "权限"};
private static JPanel panel = new JPanel();//子页面内容面板
// panel.setLayout(new BorderLayout());//设置布局
private static JTable t = null;
private static JLabel label = new JLabel("用户名:");
private static JButton button = new JButton("查询");
private static JTextField field = new JTextField(12);
private static JScrollPane jScrollPane = new JScrollPane(t);
private static JPanel panel2 = new JPanel();
private static JLabel jLabel = new JLabel("用户名");//用户名
private static JTextField jTextField = new JTextField(12);//用户名文本框
private static JLabel jLabel1 = new JLabel("密码");//密码
private static JPasswordField passwordField = new JPasswordField(12);//密码框
private static JButton jButton = new JButton("添加用户");
private static JButton jButton1 = new JButton("删除用户");
private static JLabel jLabel2 = new JLabel("用户权限");//用户权限
private static Object[] arr = {"普通用户", "管理员用户"};
private static JComboBox jComboBox = new JComboBox(arr);
private static JInternalFrame internalFrame = new JInternalFrame("修改密码", true, true, true, true);
private static JPanel panel1 = new JPanel();
public static void Update(JFrame frame) {
internalFrame.setSize(800, 500);//设置窗口大小
t=null;
internalFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//关闭按钮
internalFrame.setLayout(new BorderLayout());//设置子页面布局
List<User> users = Database.SelectUser();//调用数据库查询数据
String[][] heros = new String[users.size()][3];
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
heros[i][0] = user.getUser();
heros[i][1] = user.getPasswd();
if (user.getJudge() == 1) {
heros[i][2] = "管理员用户";
} else {
heros[i][2] = "普通用户";
}
}
t = new JTable(heros, columnNames);
jScrollPane = new JScrollPane(t);
panel1.add(jScrollPane);
internalFrame.updateUI();
button.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
panel1.remove(jScrollPane);
String text = field.getText();
List<User> users = Database.SelectUser(text);//调用数据库查询数据
String[][] heros = new String[users.size()][3];
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
heros[i][0] = user.getUser();
heros[i][1] = user.getPasswd();
if (user.getJudge() == 1) {
heros[i][2] = "管理员用户";
} else {
heros[i][2] = "普通用户";
}
}
t = new JTable(heros, columnNames);
jScrollPane = new JScrollPane(t);
panel1.add(jScrollPane);
internalFrame.updateUI();
}
});
panel2.setLayout(new FlowLayout());
panel2.add(jLabel);//用户名
panel2.add(jTextField);//用户名文本框
panel2.add(jLabel1);//密码
panel2.add(passwordField);//密码文本框
panel2.add(jLabel2);//用户权限
panel2.add(jComboBox);//下拉列表
panel2.add(jButton1);//删除用户
panel2.add(jButton);//添加用户
internalFrame.add(panel2, BorderLayout.SOUTH);
panel1.add(jScrollPane);
int defaultCloseOperation = internalFrame.getDefaultCloseOperation();
System.out.println(defaultCloseOperation);
// 表格上的title
panel.add(label);
panel.add(field);
panel.add(button);
internalFrame.add(panel, BorderLayout.NORTH);//窗口添加面板
internalFrame.add(panel1, BorderLayout.CENTER);//添加表格到面板中间
jDesktopPane.add(internalFrame); //讲子页面,添加到主面板
frame.add(jDesktopPane, BorderLayout.CENTER); //讲主面板添加到页面中
internalFrame.setVisible(true); //字面显示状态为true
//刷新页面
jButton1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String text = jTextField.getText();
String passwd = passwordField.getText();
int Index = jComboBox.getSelectedIndex();
Boolean aBoolean = Database.RemoveUser(text, passwd, Index);
if (aBoolean){
JOptionPane.showMessageDialog(panel2, "恭喜你,删除用户成功", "通知", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(panel2, "抱歉,删除用户失败", "警告", JOptionPane.ERROR_MESSAGE);
}
internalFrame.updateUI();
}
});
jButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String text = jTextField.getText();
String passwd = passwordField.getText();
int Index = jComboBox.getSelectedIndex();
Boolean aBoolean = Database.AddUser(text, passwd, Index);
if (aBoolean){
JOptionPane.showMessageDialog(panel2, "恭喜你,用户注册成功", "通知", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(panel2, "抱歉,用户注册失败", "警告", JOptionPane.ERROR_MESSAGE);
}
}
});
SwingUtilities.updateComponentTreeUI(Head.frame);
}
}
图书管理子界面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//图书管理系统管理员界面
public class LibraryManager {
private static JDesktopPane jDesktopPane2 = UpdatePasswd.jDesktopPane;//主页面 主面板
private static JInternalFrame LibraryFrame = new JInternalFrame("图书修改", true, true, true, true);
private static final int Size = 12;
private static JPanel panel = new JPanel();
private static JLabel label = new JLabel("图书序号:");
private static JTextField text = new JTextField(Size);
private static JPanel panel1 = new JPanel();
private static JLabel label1 = new JLabel("作 者:");
private static JTextField text1 = new JTextField(Size);
private static JPanel panel2 = new JPanel();
private static JLabel label2 = new JLabel("书 名:");
private static JTextField text2 = new JTextField(Size);
private static JPanel panel3 = new JPanel();
private static JLabel label3 = new JLabel("价 格:");
private static JTextField text3 = new JTextField(Size);
private static JPanel panel4 = new JPanel();
private static JLabel label4 = new JLabel("出 版社:");
private static JTextField text4 = new JTextField(Size);
private static JPanel panel5 = new JPanel();
private static JLabel label5 = new JLabel("出版时间:");
private static JTextField text5 = new JTextField(Size);
private static JPanel panel6 = new JPanel();
private static JLabel label6 = new JLabel("库存数量:");
private static JTextField text6 = new JTextField(Size);
private static JPanel panel7 = new JPanel();
private static JLabel label7 = new JLabel("分 类:");
private static Object[] book = {"小说", "长篇小说", "传记", "政治", "伦理", "逻辑", "文学"};
private static JComboBox bookcombobox = new JComboBox(book);
private static JButton button = new JButton("添加");
// private static JButton button1 = new JButton("删除");
private static Box box = Box.createHorizontalBox();
private static Box box2 = Box.createHorizontalBox();
private static Box box3 = Box.createHorizontalBox();
private static Box box4 = Box.createHorizontalBox();
private static Box box5 = Box.createHorizontalBox();
private static Box box12 = Box.createVerticalBox();
public static void Library() {
LibraryFrame.setLayout(new BorderLayout());//设置子页面布局 LibraryFrame弹出页面
jDesktopPane2.add(LibraryFrame);// jDesktopPane2中间面板
Head.frame.add(jDesktopPane2, BorderLayout.CENTER);
panel.add(label);
panel.add(text);
panel1.add(label1);
panel1.add(text1);
box.add(panel);
box.add(panel1);
panel3.add(label3);
panel3.add(text3);
panel2.add(label2);
panel2.add(text2);
box2.add(panel2);
box2.add(panel3);
panel4.add(label4);
panel4.add(text4);
panel5.add(label5);
panel5.add(text5);
box3.add(panel4);
box3.add(panel5);
panel6.add(label6);
panel6.add(text6);
panel7.add(label7);
panel7.add(bookcombobox);
box4.add(panel6);
box4.add(panel7);
box5.add(button);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String texts = text.getText();
String texts1 = text1.getText();
String texts2 = text2.getText();
String texts3 = text3.getText();
String texts4 = text4.getText();
String texts5 = text5.getText();
String texts6 = text6.getText();
String texts7 =(String) bookcombobox.getSelectedItem();
Boolean b = Database.AddBook(texts, texts1, texts2, texts3, texts4, texts5, texts6, texts7);
if (b){
JOptionPane.showMessageDialog(box5, "恭喜你,图书添加成功", "通知", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(box5, "抱歉,图书添加失败", "警告", JOptionPane.ERROR_MESSAGE);
}
}
});
button.setSize(100,50);
// box5.add(button1);
box12.add(box);
box12.add(box2);
box12.add(box3);
box12.add(box4);
box12.add(box5);
LibraryFrame.add(box12, BorderLayout.CENTER);
JPanel jPanel = new JPanel(new FlowLayout());
JLabel jLabel = new JLabel("图书添加");
jLabel.setFont(new Font("黑体", Font.PLAIN, 50));
jLabel.setForeground(new Color(0XFFAEB9));//设置字体颜色
jPanel.add(jLabel);
LibraryFrame.add(jPanel,BorderLayout.NORTH);
LibraryFrame.setSize(800, 500);//设置窗口大小
LibraryFrame.setVisible(true);
LibraryFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//关闭按钮
LibraryFrame.updateUI();
SwingUtilities.updateComponentTreeUI(Head.frame);
}
}
图书添加
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import GUI_Object.opjo.Book;
import javax.swing.*;
import javax.xml.crypto.Data;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
public class DropBook {
private static JDesktopPane jDesktopPane3 = UpdatePasswd.jDesktopPane;//主页面 主面板
private static JInternalFrame LibraryFrame = new JInternalFrame("图书修改", true, true, true, true);
private static final int Size = 12;
private static JPanel panel = new JPanel();
private static Box box = Box.createHorizontalBox();
private static JLabel label = new JLabel("图书名称:");
private static JTextField textField = new JTextField(12);
private static JButton button = new JButton("删除");
private static JLabel booklabel = new JLabel("书名");
private static String[] name ={"图书序号","作者","书名","价格","出版社","出版时间","库存数量","分类"};
private static JTable booktable = null;
private static JScrollPane scrollPane = null;
private static JPanel panel1 = new JPanel();
public static void Wind() {
LibraryFrame.setLayout(new BorderLayout());//设置子页面布局 LibraryFrame弹出页面
jDesktopPane3.add(LibraryFrame);// jDesktopPane3中间面板
Head.frame.add(jDesktopPane3, BorderLayout.CENTER);
box.add(label);
box.add(textField);
box.add(button);
panel.add(box);
LibraryFrame.add(panel,BorderLayout.NORTH);
List<Book> books = Database.SelectBook();
Object[][] list = new Object[books.size()][8];
for (int i=0;i<books.size();i++){
Book book = books.get(i);
list[i][0]=book.getId();
list[i][1]=book.getAuthor();
list[i][2]=book.getName();
list[i][3]=book.getPages();
list[i][4]=book.getPrice();
list[i][5]=book.getTime();
list[i][6]=book.getSize();
list[i][7]=book.getType();
}
booktable = new JTable(list,name);
scrollPane = new JScrollPane(booktable);
panel1.add(scrollPane);
LibraryFrame.add(panel1,BorderLayout.CENTER);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String text = textField.getText();
Boolean b = Database.DropBook(text);
panel1.remove(scrollPane);
if (b){
JOptionPane.showMessageDialog(LibraryFrame, "恭喜你,图书删除成功", "通知", JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(LibraryFrame, "抱歉,图书删除失败", "警告", JOptionPane.ERROR_MESSAGE);
}
List<Book> books = Database.SelectBook();
Object[][] list = new Object[books.size()][8];
for (int i=0;i<books.size();i++){
Book book = books.get(i);
list[i][0]=book.getId();
list[i][1]=book.getAuthor();
list[i][2]=book.getName();
list[i][3]=book.getPages();
list[i][4]=book.getPrice();
list[i][5]=book.getTime();
list[i][6]=book.getSize();
list[i][7]=book.getType();
}
booktable = new JTable(list,name);
scrollPane = new JScrollPane(booktable);
panel1.add(scrollPane);
LibraryFrame.add(panel1,BorderLayout.CENTER);
LibraryFrame.updateUI();
SwingUtilities.updateComponentTreeUI(Head.frame);
}
});
JOptionPane.showMessageDialog(LibraryFrame, "请输入需要删除的书本名称", "通知", JOptionPane.INFORMATION_MESSAGE);
LibraryFrame.setSize(800, 500);//设置窗口大小
LibraryFrame.setVisible(true);
LibraryFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//关闭按钮
LibraryFrame.updateUI();
SwingUtilities.updateComponentTreeUI(Head.frame);
}
}
普通用户界面
package GUI_Object.GUI;
import GUI_Object.mysql.Database;
import GUI_Object.opjo.Book;
import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
public class AverageUser {
private static JFrame frame = new JFrame("普通用户界面");
private static JDialog jDialog = new JDialog();
private static JLabel panel1 = new JLabel();
private static JPanel panel = new JPanel(new FlowLayout());
//设置标题
private static JLabel booktitle = new JLabel("普通用户界面");
//设置书名
private static JLabel booklabel = new JLabel("书名");
private static String[] name ={"图书序号","作者","书名","价格","出版社","出版时间","库存数量","分类"};
private static JTextField bookfield = new JTextField(7);
//设置作者
private static JLabel autorlabel = new JLabel("作者");
private static JTextField autorfield = new JTextField(5);
//设置类别
private static JLabel classlabel = new JLabel("类别");
private static Object[] book = {"小说", "长篇小说", "传记", "政治", "伦理", "逻辑", "文学"};
private static JComboBox bookcombobox = new JComboBox(book);
//设置按钮
private static JButton button = new JButton("查找");
//设置背景图片
private static ImageIcon imageIcon = new ImageIcon("D:\\java\\java项目\\untitled\\src\\image\\1.png");
private static JLabel imagelabel = new JLabel(imageIcon);
//设置表格
private static JTable booktable = null;
private static JScrollPane scrollPane = new JScrollPane(booktable);
//借阅书籍
private static JButton borrowbutton = new JButton("借阅书籍");
//归还书籍
private static JButton returnbutton = new JButton("归还书籍");
//退出系统
private static JButton quitbutton = new JButton("退出登录");
private static JPanel panel2 = new JPanel();
private static JPanel panel3 = new JPanel();
public static void Subpage() {
//标题
frame.setLayout(new BorderLayout());
booktitle.setFont(new Font("宋体", Font.PLAIN, 20));
panel.add(booktitle);//标题标签
//书名
panel.add(booklabel);//书名标签
panel.add(bookfield);//书名搜索框
//作者
panel.add(autorlabel);
panel.add(autorfield);
//类别
panel.add(classlabel);
panel.add(bookcombobox);
//按钮
panel.add(button);
button.setBackground(Color.getHSBColor(212, 179, 204));
//背景图片
// panel.add(imagelabel); //图片会默认显示在中间会覆盖掉一个容器,得把图片设置为背景,不能单独放到容器里面
//表格
panel3.add(scrollPane);
//借阅书籍
panel2.add(borrowbutton);
//归还书籍
panel2.add(returnbutton);
//退出系统
panel2.add(quitbutton);
//查找
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookName = bookfield.getText();
String text = autorfield.getText();
String Item = (String)bookcombobox.getSelectedItem();
panel3.remove(scrollPane);
List<Book> books = Database.SelectBook(bookName, text, Item);
Object[][] list = new Object[books.size()][8];
for (int i=0;i<books.size();i++){
Book book = books.get(i);
list[i][0]=book.getId();
list[i][1]=book.getAuthor();
list[i][2]=book.getName();
list[i][3]=book.getPages();
list[i][4]=book.getPrice();
list[i][5]=book.getTime();
list[i][6]=book.getSize();
list[i][7]=book.getType();
}
booktable = new JTable(list,name);
scrollPane = new JScrollPane(booktable);
panel3.add(scrollPane);
SwingUtilities.updateComponentTreeUI(frame);
}
});
//容器添加
frame.add(panel, BorderLayout.NORTH);//容器中间
frame.add(panel3, BorderLayout.CENTER);
frame.add(panel2, BorderLayout.SOUTH);//容器顶端
Head.frame.setVisible(false);
Head.initFrame(frame);
//退出登录
quitbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
frame.setVisible(false);
Head.frame.setVisible(true);
}
});
//借阅书籍
borrowbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String s = JOptionPane.showInputDialog("请输入需要借阅的书籍编号:");
// Database.BorrowAdd(s,Head.UserName,time);
// JOptionPane.showConfirmDialog(frame,"你去吗");
String time = JOptionPane.showInputDialog("请输入借阅的天数:");
boolean b = Database.BorrowAdd(s, Head.UserName, time);
if(b){
JOptionPane.showMessageDialog(panel, "借阅成功", "通知", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(panel, "借阅失败,图书编号错误", "警告", JOptionPane.WARNING_MESSAGE);
}
}
});
returnbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String s = JOptionPane.showInputDialog("请输入需要借阅的书籍编号:");
boolean b = Database.DropBorrow(s);
if (b){
JOptionPane.showMessageDialog(panel, "还书成功", "通知", JOptionPane.INFORMATION_MESSAGE);
}else {
JOptionPane.showMessageDialog(panel, "没有需要还书的书本", "通知", JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
}
实体类
package GUI_Object.opjo;
import javax.xml.crypto.Data;
import java.util.Date;
public class Book {
private int id;//图书序号
private String author;//作者
private String name;//书名
private int pages;//价格
private String price;//出版社
private Date time;//出版时间
private Double size;//库存数量
private String type;//分类
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public Double getSize() {
return size;
}
public void setSize(Double size) {
this.size = size;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", author='" + author + '\'' +
", name='" + name + '\'' +
", pages=" + pages +
", price='" + price + '\'' +
", time=" + time +
", size=" + size +
", type='" + type + '\'' +
'}';
}
}
package GUI_Object.opjo;
public class User {
private String user;//用户
private String passwd;//密码
private int judge;//判断用户权限
public User() {
}
public User(String user, String passwd, int judge) {
this.user = user;
this.passwd = passwd;
this.judge = judge;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public int getJudge() {
return judge;
}
public void setJudge(int judge) {
this.judge = judge;
}
@Override
public String toString() {
return "User{" +
"user='" + user + '\'' +
", passwd='" + passwd + '\'' +
", judge=" + judge +
'}';
}
}
数据的增删改查
需要导入mysql的驱动架包(不懂的可以去网上找,也可以私信我)
package GUI_Object.mysql;
import GUI_Object.opjo.Book;
import GUI_Object.opjo.User;
import Object_GUI.GUI.Tool;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Database {
private static final String user = "root";
private static final String passwd = "123456";
private static final String driver = "com.mysql.cj.jdbc.Driver";
private static final String url = "jdbc:mysql://127.0.0.1:3306/library1?useServerPrepStmts=true&serverTimezone=GMT";
//数据库的创建账户
public static void CreateAccount() {
Connection conn = null;
Statement state = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
state = conn.createStatement();
String sql = "create table if not exists user(username char(11) unique,passwd char(20),judge int)";
int i = state.executeUpdate(sql);
String sql2 = "insert into user value('root','123456',1)";
state.executeUpdate(sql2);
} catch (ClassNotFoundException e) {
System.out.println("数据库创建失败...");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("数据库创建成功");
}
//添加用户
public static Boolean AddUser(String text, String password, int judge) {
Connection conn = null;
PreparedStatement pared = null;
int i = 0;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql = "insert into user value(?,?,?)";
pared = conn.prepareStatement(sql);
pared.setString(1, text);
pared.setString(2, password);
pared.setInt(3, judge);
i = pared.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pared.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (i > 0) {
return true;
} else {
return false;
}
}
//查询账号密码
public static int QueryUser(String name, String password) {
Connection conn = null;
PreparedStatement pst = null;
int i = -1;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql = "select * from user where username=? and passwd=?";
pst = conn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, password);
ResultSet resultSet = pst.executeQuery();
boolean next = resultSet.next();
i = resultSet.getInt(3);//返回第三个值,判断用户权限
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
System.err.println("密码输入错误");
} finally {
try {
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return i;
}
public static List<User> SelectUser(String text) {
Connection conn = null;
PreparedStatement pst = null;
List<User> Users = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql = "select * from user";
String sql1 = "select * from user where username = ?";
pst = conn.prepareStatement(sql);
if (text != null && !text.equals("")) {
pst = conn.prepareStatement(sql1);
pst.setString(1, text);
}
ResultSet resultSet = pst.executeQuery();
Users = new ArrayList<>();
while (resultSet.next()) {
User user = new User();
user.setUser(resultSet.getString(1));
user.setPasswd(resultSet.getString(2));
user.setJudge(resultSet.getInt(3));
Users.add(user);
}
} catch (Exception e) {
System.out.println("数据查询错误");
} finally {
try {
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return Users;
}
public static List<User> SelectUser() {
Connection conn = null;
PreparedStatement pst = null;
List<User> Users = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql = "select * from user";
pst = conn.prepareStatement(sql);
ResultSet resultSet = pst.executeQuery();
Users = new ArrayList<>();
while (resultSet.next()) {
User user = new User();
user.setUser(resultSet.getString(1));
user.setPasswd(resultSet.getString(2));
user.setJudge(resultSet.getInt(3));
Users.add(user);
}
} catch (Exception e) {
System.out.println("数据查询错误");
} finally {
try {
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return Users;
}
//删除数据
public static Boolean RemoveUser(String text, String password, int index) {
Connection conn = null;
Statement statement = null;
int i = 0;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql = "delete from user where username='" + text + "' and passwd='" + password + "' and judge='" + index+"'";
statement = conn.createStatement();
i = statement.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (i > 0) {
return true;
} else {
return false;
}
}
public static List<Book> SelectBook(String bookName, String text, String item) {
Connection conn = null;
Statement statement = null;
String sql = "select * from book where type= '" + item+"'";
String sql2 = "select * from book where author='"+text+"' and type='"+item+"'";
String sql3 ="select * from book where name='"+bookName+"' and type='"+item+"'";
String sql1 = "select * from book where name='" + bookName + "' and author='" + text + "' and type='" + item+"'";
List<Book> books = new ArrayList<>();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
statement = conn.createStatement();
ResultSet resultSet = null;
if (bookName.equals("") && text.equals("")) {
resultSet = statement.executeQuery(sql);
}else
if (bookName.equals("")) {
resultSet = statement.executeQuery(sql2);
}else if(text.equals("")){
resultSet = statement.executeQuery(sql3);
}else {
resultSet = statement.executeQuery(sql1);
}
while (resultSet.next()){
Book book = new Book();
book.setId(resultSet.getInt(1));
book.setAuthor(resultSet.getString(2));
book.setName(resultSet.getString(3));
book.setPages(resultSet.getInt(4));
book.setPrice(resultSet.getString(5));
book.setTime(resultSet.getDate(6));
book.setSize(resultSet.getDouble(7));
book.setType(resultSet.getString(8));
books.add(book);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return books;
}
public static List<Book> SelectBook() {
Connection conn = null;
Statement statement = null;
String sql = "select * from book ";
List<Book> books = new ArrayList<>();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
Book book = new Book();
book.setId(resultSet.getInt(1));
book.setAuthor(resultSet.getString(2));
book.setName(resultSet.getString(3));
book.setPages(resultSet.getInt(4));
book.setPrice(resultSet.getString(5));
book.setTime(resultSet.getDate(6));
book.setSize(resultSet.getDouble(7));
book.setType(resultSet.getString(8));
books.add(book);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return books;
}
//借阅数据
public static boolean BorrowAdd(String s,String UserName,String time){
Connection conn=null;
Statement sta=null;
int i=0;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
sta = conn.createStatement();
String sql="insert into borrow(id,create_time,update_time,username) values("+s+",now(),"+time+","+UserName+")";
i = sta.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
sta.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(i>0){
return true;
}else {
return false;
}
}
public static Boolean AddBook(String texts, String texts1, String texts2, String texts3, String texts4, String texts5, String texts6, String texts7) {
Connection conn=null;
PreparedStatement stmt=null;
int i=0;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql="insert into book value(?,?,?,?,?,?,?,?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1,texts);
stmt.setString(2,texts1);
stmt.setString(3,texts2);
stmt.setString(4,texts3);
stmt.setString(5,texts4);
stmt.setString(6,texts5);
stmt.setString(7,texts6);
stmt.setString(8,texts7);
i = stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(i>0){
return true;
}else {
return false;
}
}
public static Boolean DropBook(String text) {
Connection conn=null;
PreparedStatement pstat=null;
int i=0;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
String sql = "delete from book where name=?";
pstat = conn.prepareStatement(sql);
pstat.setString(1,text);
i = pstat.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
pstat.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (i>0){
return true;
}else {
return false;
}
}
public static boolean DropBorrow(String s) {
int i=0;
Connection conn=null;
PreparedStatement pstat=null;
try {
Class.forName(driver);
conn= DriverManager.getConnection(url,user,passwd);
String sql="delete from borrow where id=?";
pstat= conn.prepareStatement(sql);
pstat.setString(1,s);
i= pstat.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
pstat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(i>0){
return true;
}else{
return false;
}
}
}
mysql数据库
create database library1;
use library1;
create table Book(
id int(11) not null primary key,-- 图书序号
author varchar(255) not null ,-- 作者
name varchar(255) not null ,-- 书名
pages int(11) NOT NULL , -- 价格
price varchar(255) not null , -- 出版社
time datetime(6) not null ,-- 出版时间
size int(11) not null ,-- 库存数量
type varchar(255) not null -- 分类
);
create table borrow(
id int(11) not null primary key ,-- 图书编号
create_time datetime(6) NOT NULL ,-- 借阅时间
update_time int(255) not null ,-- 借阅天数
username int(11) not null ,-- 借书用户
end_time datetime(6) -- 还书时间
);
drop table borrow;
alter table borrow add constraint connet foreign key(id) references book(id);
drop table user;
delete from user where username='1233';
select *from book;
use library;
insert into book values
(1,'路遥','平凡的世界',69.5,'北京十月文艺出版社','2017-07-03',4,'小说'),
(2,'霍达','穆斯林的葬礼',39.5,'北京十月文艺出版社','1988-01-03',2,'小说'),
(3,'村上春树','挪威的森林',14.8,'敦煌文艺出版社','1987-05-03',5,'小说'),
(4,'陈忠实','白鹿原',70.0,'人民文学出版社','1993-02-03',1,'小说'),
(5,'钱钟书','围城',66.5,'人民文学出版社','1991-02-09',2,'小说'),
(6,'斯托夫人','汤姆叔叔的小屋',44.5,'哈尔滨出版社','1852-07-01',4,'小说'),
(7,'余华','活着',33.0,'作家出版社','1992-03-03',7,'小说'),
(8,'塞林格','麦田里的守望者',45.3,'译林出版社','1951-02-01',2,'小说'),
(9,'曹禺','曹禺剧本选',39.5,'人民文学出版社','1997-10-08',3,'小说');
insert into book values
(10,'路遥','平凡的世界',69.5,'北京十月文艺出版社','2017-07-03',4,'小说'),
(11,'霍达','穆斯林的葬礼',39.5,'北京十月文艺出版社','1988-01-03',2,'传记'),
(12,'村上春树','挪威的森林',14.8,'敦煌文艺出版社','1987-05-03',5,'文学'),
(13,'陈忠实','白鹿原',70.0,'人民文学出版社','1993-02-03',1,'逻辑'),
(14,'钱钟书','围城',66.5,'人民文学出版社','1991-02-09',2,'长篇小说'),
(15,'斯托夫人','汤姆叔叔的小屋',44.5,'哈尔滨出版社','1852-07-01',4,'小说'),
(16,'余华','活着',33.0,'作家出版社','1992-03-03',7,'伦理'),
(17,'塞林格','麦田里的守望者',45.3,'译林出版社','1951-02-01',2,'政治'),
(18,'曹禺','曹禺剧本选',39.5,'人民文学出版社','1997-10-08',3,'传记');