药品库存管理Java Swing代码
时间: 2023-07-03 12:33:27 浏览: 98
Java Swing库存管理系统
4星 · 用户满意度95%
以下是一个简单的药品库存管理Java Swing代码示例:
```java
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class DrugInventoryManager extends JFrame implements ActionListener {
private ArrayList<Drug> drugList;
private JLabel nameLabel, quantityLabel;
private JTextField nameField, quantityField;
private JButton addButton, removeButton, updateButton;
public DrugInventoryManager() {
this.setTitle("药品库存管理");
this.setSize(400, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drugList = new ArrayList<Drug>();
nameLabel = new JLabel("药品名称:");
quantityLabel = new JLabel("药品数量:");
nameField = new JTextField(20);
quantityField = new JTextField(10);
addButton = new JButton("添加");
removeButton = new JButton("删除");
updateButton = new JButton("更新");
addButton.addActionListener(this);
removeButton.addActionListener(this);
updateButton.addActionListener(this);
JPanel panel = new JPanel();
panel.add(nameLabel);
panel.add(nameField);
panel.add(quantityLabel);
panel.add(quantityField);
panel.add(addButton);
panel.add(removeButton);
panel.add(updateButton);
this.add(panel);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
int quantity = Integer.parseInt(quantityField.getText());
drugList.add(new Drug(name, quantity));
JOptionPane.showMessageDialog(null, "药品添加成功!");
nameField.setText("");
quantityField.setText("");
} else if (e.getSource() == removeButton) {
String name = nameField.getText();
for (Drug drug : drugList) {
if (drug.getName().equals(name)) {
drugList.remove(drug);
JOptionPane.showMessageDialog(null, "药品删除成功!");
nameField.setText("");
quantityField.setText("");
return;
}
}
JOptionPane.showMessageDialog(null, "药品不存在!");
} else if (e.getSource() == updateButton) {
String name = nameField.getText();
int quantity = Integer.parseInt(quantityField.getText());
for (Drug drug : drugList) {
if (drug.getName().equals(name)) {
drug.setQuantity(quantity);
JOptionPane.showMessageDialog(null, "药品更新成功!");
nameField.setText("");
quantityField.setText("");
return;
}
}
JOptionPane.showMessageDialog(null, "药品不存在!");
}
}
public static void main(String[] args) {
new DrugInventoryManager();
}
}
class Drug {
private String name;
private int quantity;
public Drug(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
```
该示例实现了药品库存管理系统的基本功能,包括添加、删除和更新药品信息。用户可以通过输入药品名称和数量,来对药品库存进行管理。同时,系统会根据用户的操作结果,给出相应的提示信息。
阅读全文