javaswing 实现图书管理系统并运用枚举范型用代码来写JAVA代码
时间: 2023-12-23 13:04:06 浏览: 67
好的,下面是一个简单的Java Swing图书管理系统的示例代码,其中包含了枚举和泛型的使用。
首先是枚举类型的定义,用于表示图书的类型:
```java
public enum BookType {
NOVEL("小说"),
ESSAY("散文"),
POETRY("诗歌");
private String typeName;
private BookType(String typeName) {
this.typeName = typeName;
}
public String getTypeName() {
return typeName;
}
}
```
接下来是泛型类的定义,用于表示图书的信息:
```java
public class Book<T> {
private String name;
private T author;
private BookType type;
public Book(String name, T author, BookType type) {
this.name = name;
this.author = author;
this.type = type;
}
public String getName() {
return name;
}
public T getAuthor() {
return author;
}
public BookType getType() {
return type;
}
}
```
然后是图书管理系统的主界面代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class BookManagementSystem extends JFrame implements ActionListener {
private JList<Book<String>> bookList;
private ArrayList<Book<String>> books;
public BookManagementSystem() {
super("图书管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
books = new ArrayList<>();
books.add(new Book<>("Java编程思想", "Bruce Eckel", BookType.NOVEL));
books.add(new Book<>("Thinking in Java", "Bruce Eckel", BookType.NOVEL));
books.add(new Book<>("Java核心技术", "Cay S. Horstmann", BookType.NOVEL));
bookList = new JList<>(books.toArray(new Book[0]));
add(new JScrollPane(bookList), BorderLayout.CENTER);
JPanel panel = new JPanel();
add(panel, BorderLayout.NORTH);
JLabel nameLabel = new JLabel("书名:");
panel.add(nameLabel);
JTextField nameTextField = new JTextField(10);
panel.add(nameTextField);
JLabel authorLabel = new JLabel("作者:");
panel.add(authorLabel);
JTextField authorTextField = new JTextField(10);
panel.add(authorTextField);
JLabel typeLabel = new JLabel("类型:");
panel.add(typeLabel);
JComboBox<BookType> typeComboBox = new JComboBox<>(BookType.values());
panel.add(typeComboBox);
JButton addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
String author = authorTextField.getText();
BookType type = (BookType) typeComboBox.getSelectedItem();
books.add(new Book<>(name, author, type));
bookList.setListData(books.toArray(new Book[0]));
}
});
panel.add(addButton);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args) {
new BookManagementSystem();
}
}
```
以上代码中,我们使用了JList组件来展示图书列表,使用了JComboBox组件来选择图书类型,使用了JButton组件来添加新的图书。通过这个简单的示例,你可以了解到如何在Java Swing中使用枚举和泛型,实现一个基本的图书管理系统。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)