图书管理系统netbeans编写javagui界面
时间: 2024-01-18 19:17:24 浏览: 103
以下是使用NetBeans编写Java GUI界面的图书管理系统的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookManagementSystemGUI extends JFrame {
private JLabel titleLabel;
private JLabel authorLabel;
private JLabel pagesLabel;
private JLabel publishDateLabel;
private JLabel isbnLabel;
private JTextField titleTextField;
private JTextField authorTextField;
private JTextField pagesTextField;
private JTextField publishDateTextField;
private JTextField isbnTextField;
private JButton addButton;
private JButton updateButton;
private JButton deleteButton;
public BookManagementSystemGUI() {
// 设置窗口标题
setTitle("图书管理系统");
// 创建组件
titleLabel = new JLabel("书名:");
authorLabel = new JLabel("作者:");
pagesLabel = new JLabel("页数:");
publishDateLabel = new JLabel("出版日期:");
isbnLabel = new JLabel("ISBN:");
titleTextField = new JTextField();
authorTextField = new JTextField();
pagesTextField = new JTextField();
publishDateTextField = new JTextField();
isbnTextField = new JTextField();
addButton = new JButton("添加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
// 设置布局管理器
setLayout(new GridLayout(6, 2));
// 添加组件到容器中
add(titleLabel);
add(titleTextField);
add(authorLabel);
add(authorTextField);
add(pagesLabel);
add(pagesTextField);
add(publishDateLabel);
add(publishDateTextField);
add(isbnLabel);
add(isbnTextField);
add(addButton);
add(updateButton);
add(deleteButton);
// 添加按钮点击事件监听器
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 添加图书的逻辑
String title = titleTextField.getText();
String author = authorTextField.getText();
int pages = Integer.parseInt(pagesTextField.getText());
String publishDate = publishDateTextField.getText();
String isbn = isbnTextField.getText();
// 调用图书添加方法
BookDAO bookDAO = new BookDAO();
Book book = new Book(title, author, pages, publishDate, isbn);
bookDAO.addBook(book);
// 清空输入框
titleTextField.setText("");
authorTextField.setText("");
pagesTextField.setText("");
publishDateTextField.setText("");
isbnTextField.setText("");
// 弹出提示框
JOptionPane.showMessageDialog(null, "图书添加成功!");
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 修改图书的逻辑
// ...
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 删除图书的逻辑
// ...
}
});
// 设置窗口大小和可见性
setSize(400, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// 创建图书管理系统GUI对象
BookManagementSystemGUI gui = new BookManagementSystemGUI();
}
}
```
阅读全文