Java 图书管理系统代码
时间: 2024-06-16 12:00:38 浏览: 115
Java 图书管理系统是一个用Java语言开发的图书管理软件,主要用于对图书进行管理、查询和统计。下面是Java图书管理系统的一些代码实现:
1. 登录界面代码实现:
```java
public class Login extends JFrame implements ActionListener {
JLabel label1, label2;
JTextField text;
JPasswordField password;
JButton button1, button2;
public Login() {
setTitle("图书管理系统登录");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
label1 = new JLabel("用户名:");
panel.add(label1);
text = new JTextField(10);
panel.add(text);
label2 = new JLabel("密 码:");
panel.add(label2);
password = new JPasswordField(10);
panel.add(password);
button1 = new JButton("登录");
button2 = new JButton("重置");
panel.add(button1);
panel.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
getContentPane().add(panel);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String user = text.getText();
String pass = new String(password.getPassword());
if (user.equals("admin") && pass.equals("admin")) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
} else if (e.getSource() == button2) {
text.setText("");
password.setText("");
}
}
public static void main(String[] args) {
Login login = new Login();
login.setVisible(true);
}
}
```
2. 图书添加代码实现:
```java
public class AddBook extends JFrame implements ActionListener {
JLabel label1, label2, label3, label4, label5, label6;
JTextField text1, text2, text3, text4, text5;
JTextArea text6;
JButton button1, button2;
public AddBook() {
setTitle("添加图书");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(7, 2));
label1 = new JLabel("编号:");
panel.add(label1);
text1 = new JTextField(10);
panel.add(text1);
label2 = new JLabel("书名:");
panel.add(label2);
text2 = new JTextField(10);
panel.add(text2);
label3 = new JLabel("作者:");
panel.add(label3);
text3 = new JTextField(10);
panel.add(text3);
label4 = new JLabel("出版社:");
panel.add(label4);
text4 = new JTextField(10);
panel.add(text4);
label5 = new JLabel("出版日期:");
panel.add(label5);
text5 = new JTextField(10);
panel.add(text5);
label6 = new JLabel("简介:");
panel.add(label6);
text6 = new JTextArea(5, 20);
JScrollPane scrollPane = new JScrollPane(text6);
panel.add(scrollPane);
button1 = new JButton("添加");
button2 = new JButton("取消");
panel.add(button1);
panel.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
getContentPane().add(panel);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
String id = text1.getText();
String name = text2.getText();
String author = text3.getText();
String publisher = text4.getText();
String publishDate = text5.getText();
String summary = text6.getText();
// 将图书信息保存到数据库中
JOptionPane.showMessageDialog(null, "添加成功!");
dispose();
} else if (e.getSource() == button2) {
dispose();
}
}
public static void main(String[] args) {
AddBook addBook = new AddBook();
addBook.setVisible(true);
}
}
```
阅读全文